1

我有这个网址

http://mysite.com/profile?id=78

我想把它改成这样

http://mysite.com/78/

我该怎么做?

我的 htaccess 中有这段代码,但它似乎不起作用。

RewriteRule ^id/([a-zA-Z0-9]+)/$ profile?id=$1

请指导我这个。

您的帮助将不胜感激和奖励。

谢谢!

4

4 回答 4

3

我很确定您应该使用该RewriteBase指令

RewriteEngine On
RewriteBase /
# Use the following rule if you want to make the page like a directory
RewriteRule ^u/(!(profile.php))$ u/$1/ [R=301,L]
# The following rule does the rewrite.
RewriteRule ^u/(!(profile.php))/$ profile.php?name=$1 [L]

这将重写http://mysite.com/u/john/http://mysite.com/profile?name=john.

编辑这是新的答案

RewriteEngine On
RewriteBase /
# Use the following rule if you want to make the page like a directory
RewriteRule ^u/(!(profile.php))$ u/$1/ [R=301,L]
# The following rule does the rewrite.
RewriteRule ^u/(!(profile.php))/$ profile.php?name=$1 [L]
# 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$ u/%1? [R=301,L]
于 2012-07-03T03:13:06.963 回答
1

你所拥有的似乎是正确的。但是,该请求可能会再次得到处理。

您可以通过添加[L]标志并确保映射到确切的文件来避免这种情况。例如:

RewriteEngine On

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

注意:我还使尾部斜杠成为可选的。我明确地把RewriteEngine On

于 2012-07-03T03:11:03.010 回答
0

您需要在.htaccess文件中使用它:

RewriteEngine On
RewriteRule ^([^/]*)/$ /profile?name=$1 [L]

我用它来生成它:http ://www.generateit.net/mod-rewrite/

于 2012-07-03T03:10:43.447 回答
0

我用这种方式解决了它:

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?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$ %1?
于 2012-07-03T07:31:24.067 回答