Php 在htaccess中使用重写的友好URL

Php 在htaccess中使用重写的友好URL,php,.htaccess,mod-rewrite,Php,.htaccess,Mod Rewrite,我有这个网址 http://mysite.com/profile?id=78 我想把它改成这样 http://mysite.com/78/ 我该怎么做 我的htaccess中有这个代码,但它似乎不起作用 RewriteRule ^id/([a-zA-Z0-9]+)/$ profile?id=$1 请在这一点上指导我 你的帮助将得到极大的感激和回报 谢谢 您需要在.htaccess文件中使用此选项: 我使用它来生成它:您需要在.htaccess文件中使用它: 我用它来生成它:您所做的看起来是

我有这个网址

http://mysite.com/profile?id=78
我想把它改成这样

http://mysite.com/78/
我该怎么做

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

RewriteRule ^id/([a-zA-Z0-9]+)/$ profile?id=$1
请在这一点上指导我

你的帮助将得到极大的感激和回报


谢谢

您需要在.htaccess文件中使用此选项:


我使用它来生成它:

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


我用它来生成它:

您所做的看起来是正确的。但是,请求可能会再次得到处理

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

RewriteEngine On

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

注意:我还将尾部斜杠设置为可选。我明确地打开了重写引擎,你所做的看起来是正确的。但是,请求可能会再次得到处理

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

RewriteEngine On

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

注意:我还将尾部斜杠设置为可选。我明确地打开了RewriteEngine,我非常确定您应该使用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]

我非常确定您应该使用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]

我用这种方法解决了这个问题:

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?

我用这种方法解决了这个问题:

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?

这是一条非常开放的规则。最好让它们更具体一些。我同意。但是OP希望这样,对吗?希望他只提供名字,这是一条非常开放的规则。最好让它们更具体一些。我同意。但是OP希望这样,对吗?希望他应该只给出名字,但我用另一种方式,比如:从到@ElsonSolano这就是你需要的。当我们说“重写”时,它意味着浏览器可以看到http://mysite.com/u/john/ 但是看里面http://mysite.com/profile?name=john. 但用户仍然可以看到干净的URL。@ElsonSolano可以查看这是否是您想要的want@ElsonSolano如果你的站点还提供其他功能,那么删除u/将是不明智的,因为它可能会简单地阻止其他功能well@AlvinWong:没关系,我的网站不会阻止其他功能。它真的打算有这样一种格式:但我用另一种方式,比如:从到@ElsonSolano这就是你需要的。当我们说“重写”时,它意味着浏览器可以看到http://mysite.com/u/john/ 但是看里面http://mysite.com/profile?name=john. 但用户仍然可以看到干净的URL。@ElsonSolano可以查看这是否是您想要的want@ElsonSolano如果你的站点还提供其他功能,那么删除u/将是不明智的,因为它可能会简单地阻止其他功能well@AlvinWong:没关系,我的网站不会阻止其他功能。它的真正意图是有这样一种格式:你知道如何以多种方式来做这件事吗?比如,我也想在我的category.php上实现这一点,我试图复制下面的代码,并将profile.php替换为category.php,但实际情况是profile.php将我重定向到category.php。你知道吗?你知道如何用多种方式做这件事吗?比如,我也想在我的category.php上实现这一点,我试图复制下面的代码,并将profile.php替换为category.php,但实际情况是profile.php将我重定向到category.php。你知道吗?