【发布时间】:2020-09-13 17:37:54
【问题描述】:
我在这个问题中添加了 50 的赏金。我正在努力做到这一点:
- 所有网站页面都显示 www.example.com/page/ - 而不是 /page。我下面的当前代码仅适用于 www.example.com/page
如何更正 .htaccess 代码以实现此目的?
此外,将规范 URL 元标记从 www.mysite.com/page 更改为 www.example.com/page/ - 将 www.example.com/page/ 显示在搜索引擎中而不是 /page ?p>
当前的.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]
注意第一个重写条件只是转发到www。和 https,所以唯一可能需要编辑的部分可能是:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
所以我想做的只是添加一个'/'。在同一个网站(位于 www.example.com/blog/)上实现我想要的 wordpress 博客(可能是 PHP 文件而不是 HTML)的 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
我不确定这是如何工作的,但我很想了解更多关于 .htaccess 和重定向的信息,但对于初学者来说,网络上似乎没有什么内容。我也读过this 的帖子。它也必须是 https(不知道为什么,但博客文章也可以有一个 http 链接,而其他页面重定向到 https)。
该网站上还有一个博客(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
【问题讨论】:
标签: php regex .htaccess redirect url-rewriting