URL 重写不会自动重写 [英] URL Rewriting doesn't rewrite automatically
问题描述
我的网站上有这个简单的重定向,它不会自动重写我的 url,我必须手动输入其他 URL 才能访问它,可能是什么问题,我是否需要在 php.ini 中启用某些内容,如下所示是重写规则
I have this simple redirection on my website and it does not rewrite my url automatically, I have to type manual other URL to access it, what could be the problem, do I need to have something enabled in php.ini, below is the rewrite rule
RewriteEngine On
RewriteRule ^new-page-([^/]*)\.php$ /mypage.php?name=$1 [L]
推荐答案
如果您在 .htaccess
中进行此操作,您还应该指定 RewriteBase/
.试试
If you are doing it in .htaccess
you should also specify RewriteBase /
.
Try
RewriteEngine On
RewriteBase /
RewriteRule ^new-page-([^/]*)\.php$ mypage.php?name=$1 [L]
这会在内部将 new-page-something.php
重写为 mypage.php?name=something
.
This rewrites new-page-something.php
tomypage.php?name=something
internally.
添加:
也许你想要这个:
RewriteEngine On
RewriteBase /
RewriteRule ^new-page-([^/]*)\.php$ mypage.php?name=$1 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /mypage.php
RewriteCond %{QUERY_STRING} name=([^\&]*)
RewriteRule ^mypage.php$ new-page-%1.php? [R,L]
第一个添加的 RewriteCond 检查真正的请求是否正在请求 /mypage.php
以防止重定向循环.添加的第二个 RewriteCond 用于匹配 RewriteRule 中使用的查询字符串.
The first added RewriteCond checks whether the real request is requesting for /mypage.php
to prevent a redirect loop.
The second added RewriteCond is used to match the query string for use in RewriteRule.
这篇关于URL 重写不会自动重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!