重命名Url .htaccess

时间:2013-04-03 01:12:43

标签: php .htaccess mod-rewrite

我在使用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

我该怎么做?

3 个答案:

答案 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/$varmywebsite.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