我在使用mod-rewrite重写这些url时遇到了麻烦:
mywebsite.com/index.php?page=folder/file
mywebsite.com/index.php?page=folder/file&id=10
进入这些
mywebsite.com/file
mywebsite.com/file/10
我该怎么做?
答案 0 :(得分:2)
你想要类似的东西
#With mod_rewrite
RewriteEngine on
RewriteRule ^/file/(.+) /index.php?page=folder/file&id=$1 [R,L]
RewriteRule ^/file /index.php?page=folder/file [R,L]
答案 1 :(得分:2)
mywebsite.com/index.php?page=folder/file
mywebsite.com/index.php?page=folder/file&id=10
进入这些mywebsite.com/file
mywebsite.com/file/10
目前尚不清楚文件夹是固定字符串还是 file 所在的目录,因此在本回答中假定它是一个固定字符串作为目录两个案例都是root。
您可以在根目录下的一个.htaccess文件中尝试:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ^([^/]+)/?$ /index.php?page=folder/$1 [L,NC]
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ^([^/]+)/([^/]+)/? /index.php?page=folder/$1&id=$2 [L,NC]
静默地映射:
http://mywebsite.com/file
到
http://mywebsite.com/index.php?page=folder/file
和
http://mywebsite.com/file/10
到
http://mywebsite.com/index.php?page=folder/file&id=10
假设字符串文件和 10 是动态的。
对于永久重定向,将[L,NC]替换为[R = 301,L,NC]
答案 2 :(得分:0)
您的意思是要重写而不是将mywebsite.com/$var
重定向到mywebsite.com/index.php?page=folder/$var
和mywebsite.com/$var1/$var2
进入mywebsite.com/index.php?page=folder/$var1&id=$var2
吗?你可以查看:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([a-z0-9-_]+)$ /index.php?page=folder/$1
RewriteRule ^([a-z0-9-_]+)/([a-z0-9-_]+)$ /index.php?page=folder/$1&id=$2