1
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 的内容。

我应该在这里做什么?任何帮助将不胜感激和奖励!

谢谢!

4

3 回答 3

1

我在你的规则中遇到了问题。我认为的问题是您的网络服务器无法区分请求是类别页面/个人资料页面,只要请求来自 http://mysite.com/john/

所以我建议在个人资料页面请求或类别页面请求中进行一次更改。

只需添加http://mysite.com/profile/john/

希望这能解决。

于 2012-07-04T02:28:41.870 回答
1

要回答评论中修改的问题:

如果这些术语出现在 url 中,要区分用户和类别,您只需要每条规则一行(未经测试的示例):

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
于 2012-07-04T02:39:29.230 回答
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?
于 2012-07-04T02:38:32.850 回答