0

我想获取我的 .htaccess 文件并为另一个虚 URL 添加第二条规则,因为我已经有一个声明该页面的规则。那么我将如何编写它以便我可以访问我的页面,而下一个虚荣心是个人资料的变量?

Ex:// mywebsite.com/page/profile 

这是我当前的 .htaccess 文件。

RewriteEngine On

RewriteBase /

RewriteRule ^([a-zA-Z0-9-]+)$ index.php?p=$1 [L]

RewriteRule ^([a-zA-Z0-9-]+)/$ index.php?p=$1 [L]
4

1 回答 1

1

让我们举个例子。

您当前的网址是http://domain.com/myurlhttp://domain.com/index.php?p=myurl 您可以使用这种 htaccess 为该页面添加虚荣网址:

RewriteEngine On
RewriteBase /
RewriteRule ^myvanityurl/?$ myurl [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?p=$1 [L]

然后,http://domain.com/myvanityurl将重定向到http://domain.com/myurl

如果您希望此重定向是透明的(也就是保留myvanityurl在 url 栏中),请R=301从规则中删除标记。


编辑

编辑后,这就是您要搜索的内容:

RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ index.php?p=$1&profile=$2 [L]
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/$ index.php?p=$1&profile=$2 [L]

然后,http://domain.com/edit/756将重定向到http://domain.com/index.php?p=edit&profile=756

此代码可以通过以下方式简化:

RewriteEngine On
RewriteBase /
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?p=$1&profile=$2 [L]
于 2012-11-21T21:54:33.097 回答