所以首先,我有一个自定义的url重写,将请求变量发送到php脚本
重写规则如下:
RewriteRule ^(['w'/-]+)('?.*)?$ test/index.php?slug=$1 [L,T=application/x-httpd-php]
因此,如果您访问类似 domain.com/slug-text
的内容,它会slug-text
发送到位于名为 test 的文件夹中的index.php
。
想要的是我所有的网址看起来像domain.com/slug-text.html
,但slug-test
变量仍然应该发送到index.php
文件中。
和
我无法弄清楚的是重定向。我希望将所有旧网址从domain.com/slug-text
或domain.com/slug-text/
重定向到domain.com/slug-text.html
slug-text
发送到位于测试文件夹中index.php
文件。
搜索了很多,但在互联网上的任何地方都找不到这个问题的答案。
谢谢大家的帮助。
更新:我的新代码是:
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^((['w/'-]+)?['w-])(?!:'.html)$ http://domain.com/$1'.html [L,R=301]
RewriteRule ^((['w/'-]+)?['w-])(/|'.html)?$ test/index.php?slug=$1 [L]
domain.com/slug-text/
不会被重定向到domain.com/slug-text.html
domain.com/slug-text
按预期重定向到domain.com/slug-text.html
我需要更改什么?
此规则:
RewriteRule ^((['w/'-]+)?['w-])(/|'.html)?$ test/index.php?slug=$1 [L]
将困住domain.com/slug-text
,domain.com/slug-text/
并domain.com/slug-text.html
并将slug-text
发送到slug
参数内的/test/index.php
。
如果您真的想使用 [R=301]
从旧 url 重定向到新 url,请使用以下命令:
RewriteRule ^((['w/-]+)?['w-])/?(?!:'.html)$ http://domain.com/$1.html [L,R=301]
RewriteRule ^((['w/-]+)?['w-])'.html$ test/index.php?slug=$1 [L]
另请注意,由于使用显式重定向底部规则被修改为捕获 url 的结尾.html
还建议(如果您的 .htaccess 尚未包含此内容)过滤现有文件和文件夹的条件,使其不会被重定向规则捕获。只需在RewriteRule
行之前添加以下行:
# existing file
RewriteCond %{SCRIPT_FILENAME} !-f
# existing folder
RewriteCond %{SCRIPT_FILENAME} !-d
如果使用符号链接:
# enable symlinks
Options +FollowSymLinks
# existing symlink
RewriteCond %{SCRIPT_FILENAME} !-l
//加法
您的 .htaccess 文件应如下所示:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^((['w/-]+)?['w-])/?(?!:'.html)$ http://domain.com/$1.html [L,R=301]
RewriteRule ^((['w/-]+)?['w-])'.html$ test/index.php?slug=$1 [L]
应该将/slug-text重定向到/slug-text.html
RedirectMatch ^/(['w-]+)/?$ http://domein.com/$1.html
这是在 slug-text 只有字母、数字和 _ 的情况下。将 slug-text.html 重写为 php 文件,并将 slug 作为参数传递:
RewriteRule ^(['w-]+)'.html$ test/index.php?slug=$1 [R,L]
如果您的 .htaccess 中有两行,则第一行将执行从旧 URL 到新 URL 的重定向,第二行将处理请求。