2016-12-01 55 views
0

有没有办法让这个工作?.htaccess - 具有不同参数的干净网址

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^/]+)/([^/]+)$ index.php?a=1&b=2 [L] 
RewriteRule ^([^/]+)/([^/]+)$ index.php?a=$1&c=$2 [L] 

的URL看起来应该是这样:

http://test.com/a/b 
or 
http://test.com/a/c 

它仅与第一条规则的作品。

在我的情况下,我正在尝试创建一个配置文件页面,并通过$ _GET ['id']获得会话ID的ID。

所以,如果我去拜访某人的个人资料其他URL是

index.php?page=profile&id=25 
/profile/25 

如果我会去看我的个人资料是

index.php?page=profile 
/profile 

再举例来说,我想修改我的个人资料它是

index.php?page=profile&action=edit 
/profile/edit 

我希望你明白我的意思,并可以帮助我。

+0

我不确定你的两个网址之间有什么区别,说应该使用a =来重定向到哪个网址。和b =?另一个应该是a =?和c = ?. – Gerrit0

+0

例如:http://www.test.com/post/edit/或http://www.test.com/post/25 – smarvel

+0

但是,这只是不会工作,因为它总是试图去test.com/ post/25 – smarvel

回答

0

解决此问题的关键是注意您想要传递的每个参数之间的差异。

  • 如果访问者正在查看其他人的个人资料,则会传递一个id(数字)。
  • 如果访客是编辑他们的个人资料,参数字符串(字母数字)传递
  • 如果访客是看着自己的个人资料,或其他普通网页,没有多余的参数传递

.htaccess规则可最容易从最具体的书面最普遍,所以这个转换,规则成为

RewriteRule ^([^/]+)/([0-9]+)$ index.php?page=$1&id=$2 [L] 
RewriteRule ^([^/]+)/([a-z0-9]+)$ index.php?page=$1&action=$2 [NC,L] 
RewriteRule ^([^/]+)$ index.php?page=$1 [L] 

同样重要的是不过跳过现有文件&目录,因为... e RewriteCond语句只与下一个RewriteRule相匹配,最简单的做法是以与你做的稍微不同的方式来完成。

RewriteEngine On 

# If the file or directory exists, exit 
RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule .? - [END] 

RewriteRule ^([^/]+)/([0-9]+)$ index.php?page=$1&id=$2 [L] 
RewriteRule ^([^/]+)/([a-z0-9]+)$ index.php?page=$1&action=$2 [NC,L] 
RewriteRule ^([^/]+)$ index.php?page=$1 [L] 
+0

非常感谢您的回答!我会在星期一尝试这个。周末愉快! – smarvel

+0

非常感谢,它的工作原理!非常感谢您的帮助! – smarvel