我想获取我的.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]
答案 0 :(得分:1)
我们举个例子。
您当前的网址为http://domain.com/myurl
,指向http://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
),请从规则中删除标记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]