我已经阅读并遵循了指南,并寻找了其他人的问题的答案,但是我在URL重写和htaccess中苦苦挣扎。
我的网站上有一个带有索引页的子文件夹,该索引页处理查询字符串以显示动态内容。我希望变量看起来像是子文件夹名称,例如
http://mywebsite.com/subfolder/index.php?v=variable
到
http://mywebsite.com/subfolder/variable
我制作的.htaccess文件位于http://mywebsite.com/subfolder/,即与index.php相同的文件夹
我该如何使用它?任何帮助表示感谢。
答案 0 :(得分:0)
在您的.htaccess
中使用它:
RewriteEngine On
RewriteCond %{QUERY_STRING} v=(.+)$ [NC]
RewriteRule ^ subfolder/%1? [R=301,L,NE]
这将使用%{QUERY_STRING}
获取变量,然后使用%1
将其附加到重写中。您会看到我在重写的末尾添加了?
。那是为了防止原始查询出现在URL的末尾。
我使用了R=301
,它是永久重定向。您可能需要在测试时将其更改为R=302
,因为这是暂时的。
您可以在此处查看对此规则的测试:https://htaccess.madewithlove.be?share=7b6832e9-2c05-5d1d-916c-e4dd0f5b1da6
在测试之前,请确保清除缓存 。
答案 1 :(得分:0)
您可以在/subfolder/.htaccess
RewriteEngine On
RewriteBase /subfolder/
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ index.php?v=$1 [L]
RewriteEngine On
RewriteBase /subfolder/
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+)&v2=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+)\s [NC]
RewriteRule ^ %1? [R=301,L]
# Don't touch to existing files/folders
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrite /xxx to /index.php?v=xxx
RewriteRule ^([^/]+)$ index.php?v=$1 [L]
# Rewrite /xxx/yyy to /index.php?v=xxx&v2=yyy
RewriteRule ^([^/]+)/([^/]+)$ index.php?v=$1&v2=$2 [L]