这是URL
http://www.example.com/main/index.php?p=test
,我想把它变成
http://www.example.com/test
我现在的.htaccess看起来像这个
RewriteOptions inherit
RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^template\/?(.*)$ "http\:\/\/www\.example\.com\/$1" [R=301,L]
RewriteCond %{THE_REQUEST} \s/+index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteRule ^([^./]*)/?$ /index.php/?p=$1 [L,QSA]和这些规则,我只在的末尾得到这个
/主要/
我尝试过许多其他重写规则,但这个规则似乎是我能得到的最接近的规则。
我的修复
RewriteEngine On
RewriteRule ^([^/]*)\/$ /main/index.php?p=$1 [L]它不完全符合我的要求,但我是一个接近,我可以得到和工作。我们看到的不是http://www.example.com/main/index.php?p=test,而是http://www.example.com/main/test/希望使用/test,但是/test/ will就行了。我还必须把这个放在每个子文件夹中,并将/main/更改为文件夹名,但是这是自动的,所以我并不那么担心,谢谢大家的帮助。
发布于 2013-12-10 06:52:45
移除前面的斜杠。.htaccess是每个目录指令,Apache从RewriteRule URI模式中剥离当前目录路径(从而导致斜杠)。
试试这条规则:
RewriteEngine on
RewriteRule ^template/?(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/+index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteRule ^([^./]+)/?$ /index.php/?p=$1 [L,QSA]发布于 2013-12-10 07:06:13
将.htaccess文件放置在根(/)而不是主文件夹(/main)中
更新
RewriteEngine On
RewriteRule ^(.*?)$ main/index.php?p=$1 [QSA]请试一下上面的内容
发布于 2013-12-10 08:37:40
您说根目录中有此内容,因此需要将main添加到htaccess regex,否则代码将指向错误的目录。如果能用,请告诉我。
# add /main
RewriteCond %{THE_REQUEST} \s/+main/index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
#add /main
RewriteRule ^([^./]*)/?$ /main/index.php/?p=$1 [L,QSA]注意:我在我的一个网站上测试了这个,它成功了。
https://stackoverflow.com/questions/20487822
复制相似问题