I've added a bounty of 50 to this question. I'm trying to make it so that:

我在这个问题上添加了50的赏金。我试图这样做:

  1. all website pages show www.example.com/page/ - instead of /page. My current code below is only successful for www.example.com/page
  2. 所有网站页面都显示www.example.com/page/ - 而不是/ page。我目前的代码仅适用于www.example.com/page

How can the .htaccess code be corrected to achieve this?

如何纠正.htaccess代码来实现这一目标?

Also, changing the canonical URL meta tags from www.mysite.com/page to www.example.com/page/ - will www.example.com/page/ show up in search engines instead of /page ?

此外,将规范的URL元标记从www.mysite.com/page更改为www.example.com/page/ - www.example.com/page/会显示在搜索引擎中而不是/ page吗?

The current .htaccess code is:

当前的.htaccess代码是:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^ example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Note that the first rewriting conditions just forward to www. and https, so the only part that might need editing might just be:

请注意,第一次重写条件只是向前转到www。和https,所以唯一可能需要编辑的部分可能是:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

So all I want to do it simply add a '/'. A wordpress blog (presumably PHP files rather than HTML) that achieves my wants on the same website (it's at www.example.com/blog/) has htaccess code being:

所以我只想添加一个'/'。一个wordpress博客(可能是PHP文件而不是HTML)可以在同一个网站上实现我的需求(在www.example.com/blog/上),htaccess代码为:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress

I'm not sure how this works but I'd love to learn more about .htaccess and redirects, but nothing much seems to be on the web for beginners. I've also read this post. It also must be https (not sure why but blog posts also can have a http link, whereas other pages redirect to https).

我不确定这是如何工作的,但我很想了解更多关于.htaccess和重定向的信息,但对于初学者来说似乎没有什么在网上。我也读过这篇文章。它也必须是https(不知道为什么,但博客文章也可以有一个http链接,而其他页面重定向到https)。

The website also has a blog (php files) on it with its own htaccess file, installed under example.com/blog/ - the htaccess code is the default wordpress one:

该网站上还有一个博客(php文件),上面有自己的htaccess文件,安装在example.com/blog/下 - htaccess代码是默认的wordpress代码:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

# END WordPress

2 个解决方案

#1


1

Have your .htaccess as this in site root:

在站点根目录中使用.htaccess:

RewriteEngine On

# force www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

# force https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

# remove .php or .html extensions
RewriteCond %{THE_REQUEST} \s/+(.+?)\.(?:html|php)[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]

# add .html extension
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

# add .html extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

For issues with relative paths for css/js/images add this just below <head> section of your page's HTML:

对于css / js / images的相对路径问题,请在页面HTML的部分下方添加:

<base href="/" />

更多相关文章

  1. 指导我优化我的PHP代码,用数组中的空数据填充缺失值
  2. 分叉开源PHP代码,项目类加载器策略
  3. 用Hack编写的代码比在HHVM上用PHP编写的代码更快吗?
  4. PHPCMS V9 的手机门户wap绑定单页面
  5. [php]在PHP中读取和写入WORD文档的代码
  6. 使用curl加载xsl页面会返回实际的基数错误
  7. 打印具有各种值的php页面
  8. php评论回复无限极嵌套如何实现?已写基本代码,期待高手解惑
  9. advertising.php源代码分析

随机推荐

  1. go语言中自定义包的方法
  2. golang为什么那么火?
  3. Gin 中实现 HTTP Basic Auth
  4. go语言中的结构体与方法详解
  5. golang适合web开发吗?
  6. 详解 Go 中的不可变类型
  7. Golang 可以反编译吗?
  8. golang是否值得深入学习?
  9. GO语言中=和:=的区别介绍
  10. Go 语言中的 Context 详解