RewriteEngine On
RewriteBase /
# Use the following rule if you want to make the page like a directory
RewriteRule ^(v)$ /$1/
# The following rule does the rewrite.
RewriteRule ^(.+)/$ profile.php?name=$1
# The following rewrite the other way round:
RewriteCond %{REQUEST_URI} ^/profile.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php
RewriteCond %{QUERY_STRING} name=([^&]+)
RewriteRule ^profile.php$ %1?
我在我的.htaccess文件中执行上面的代码,这在我的profile.php页面中运行正常,因为之前,我的profile.php正在显示这种URL:
http://mysite.com/profile.php?name=john
但是当我写下这段代码时,这就是结果:
http://mysite.com/john/
我也想把这个代码应用到我的category.php中,所以我所做的只是复制上面的代码而我的整个.htaccess文件看起来像这样:
RewriteEngine On
RewriteBase /
# Use the following rule if you want to make the page like a directory
RewriteRule ^(v)$ /$1/
# The following rule does the rewrite.
RewriteRule ^(.+)/$ profile.php?name=$1
RewriteRule ^(.+)/$ category.php?cid=$1
# The following rewrite the other way round:
RewriteCond %{REQUEST_URI} ^/profile.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php
RewriteCond %{QUERY_STRING} name=([^&]+)
RewriteRule ^profile.php$ %1?
RewriteCond %{REQUEST_URI} ^/category.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /category.php
RewriteCond %{QUERY_STRING} cid=([^&]+)
RewriteRule ^category.php$ %1?
但是这个代码给了我一个奇怪的结果,因为当我访问时
http://mysite.com/john/
我看到的内容是我的category.php。
我该怎么办?任何帮助都将受到高度赞赏和奖励!
谢谢!
答案 0 :(得分:1)
我在你的规则中遇到了问题。我想的问题是,无论何时请求出现,您的网络服务器都无法区分请求属于类别页面/个人资料页面 http://mysite.com/john/
所以我建议在个人资料页面请求或类别页面请求中进行一次更改。
只需添加http://mysite.com/profile/john/
即可希望这会成功。
答案 1 :(得分:1)
回答评论中修改过的问题:
要区分用户和类别,如果这些字词出现在网址中,则每条规则只需要一行(未经测试的示例):
RewriteEngine On
RewriteBase /
RewriteRule ^user/(\w+)/?$ /profile.php?name=$1 // using only a limited set of chars (letters and underscore) for the user name and an optional / at the end
RewriteRule ^category/(\w+)/?$ /category.php?cid=$1 // using only a limited set of chars for the category (letters and underscore) and an optional / at the end
答案 2 :(得分:0)
看起来我用这个解决了它:
RewriteEngine On
RewriteBase /
# Use the following rule if you want to make the page like a directory
RewriteRule ^user/(!(profile.php))$ user/$1/ [R=301,L]
RewriteRule ^category/(!(category.php))$ category/$1/ [R=301,L]
# The following rule does the rewrite.
RewriteRule ^user/(.+)/$ profile.php?id=$1
RewriteRule ^category/(.+)/$ category.php?id=$1
# The following rewrite the other way round:
RewriteCond %{REQUEST_URI} ^/profile.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php
RewriteCond %{QUERY_STRING} id=([^&]+)
RewriteRule ^profile.php$ user/%1?
RewriteCond %{REQUEST_URI} ^/category.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /category.php
RewriteCond %{QUERY_STRING} id=([^&]+)
RewriteRule ^category.php$ category/%1?