Mod rewrite htaccess短url

Mod rewrite htaccess短url,mod-rewrite,Mod Rewrite,可能重复: 我试图用htaccess在我的url中隐藏动态php,现在我完全是htaccess的初学者,根据我迄今所学的知识,我成功地做到了这一点: Options +FollowSymLinks RewriteEngine on RewriteRule ^home/$ http://www.something.com/index.php?p=home [NC,L] RewriteRule ^home$ http://www.something.com/index.php?p=home [N

可能重复:

我试图用htaccess在我的url中隐藏动态php,现在我完全是htaccess的初学者,根据我迄今所学的知识,我成功地做到了这一点:

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^home/$ http://www.something.com/index.php?p=home [NC,L]
RewriteRule ^home$ http://www.something.com/index.php?p=home [NC,L]
现在发生的是当我写的时候,让我们说:

www.something.com/home

www.something.com/home/

我被重定向到了,但是我想看到的是www.something.com/home的url,而不是显示用户的完整路径,有人能告诉我我做错了什么吗


提前感谢:

如果在重写规则中写入完整url,apache将执行重定向,因为无法透明地将请求重写到其他服务器,即使完整url对应于当前服务器

最简解 对于问题中的示例,您可以简单地从重写规则中删除域名:

RewriteRule ^home/?$ /index.php?p=home [QSA,NC,L]
这将匹配URL/home或/home/并将它们重写为/index.php?p=home

处理任何url 更灵活的方法是重写所有URL,例如:

RewriteRule ^(.*)$ index.php?p=$1 [QSA,L]
它将重写当前url的任何内容,作为get参数p

一个完整的示例,它不会重写对真实文件/css/foo.css或任何其他资产的请求:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [QSA,L]
QSA标志的目的 在这两个示例中使用的标志将任何现有get参数与正在重写的新p get arg合并。因此,例如

 /foo?bar=zum
变成

/index.php?p=/foo&bar=zum

如果在重写规则中写入完整的url,apache将执行重定向,因为无法透明地将请求重写到其他服务器,即使完整的url对应于当前服务器

最简解 对于问题中的示例,您可以简单地从重写规则中删除域名:

RewriteRule ^home/?$ /index.php?p=home [QSA,NC,L]
这将匹配URL/home或/home/并将它们重写为/index.php?p=home

处理任何url 更灵活的方法是重写所有URL,例如:

RewriteRule ^(.*)$ index.php?p=$1 [QSA,L]
它将重写当前url的任何内容,作为get参数p

一个完整的示例,它不会重写对真实文件/css/foo.css或任何其他资产的请求:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [QSA,L]
QSA标志的目的 在这两个示例中使用的标志将任何现有get参数与正在重写的新p get arg合并。因此,例如

 /foo?bar=zum
变成

/index.php?p=/foo&bar=zum

这是因为您放置了完整的url。试试这个:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^home/$ index.php?p=home [NC,L]
RewriteRule ^home$ index.php?p=home [NC,L]

这是因为您放置了完整的url。试试这个:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^home/$ index.php?p=home [NC,L]
RewriteRule ^home$ index.php?p=home [NC,L]

如果您编写一个完整的URL作为替换模式,就会发生这种情况。Apache假定您想要的是重定向,而不是本地/内部重写。如果您编写一个完整的URL作为替换模式,就会发生这种情况。Apache假设您需要重定向,而不是本地/内部重写。首先非常感谢您的工作,但出于某种奇怪的原因,这完全破坏了我的网站风格,为什么会发生这种情况?@durian取决于您如何引用css文件-如果您使用首先非常感谢您的工作,但出于某种奇怪的原因,这完全破坏了我网站的风格,为什么会发生这种情况?@durian取决于你如何引用你的css文件-如果你使用