.htaccess mod_rewrite 在不同站点上有不同的结果
我正在使用 mod_rewrite 创建 SEO URL。基本上,事情按以下方式工作: 所有请求都被 .htaccess 捕获并重定向到 php 脚本(假设为transform.php)。 transform.php 解析 SEO URL 并将其转换为普通 URL,然后包含 index.php,然后捕获应用程序(index.php)的输出并使用 RegEx 将所有普通 URL 转换为 SEO URL。 所以最重要的一步是让所有请求都经过transform.php。我的 .htaccess 相当长,但我设法将其限制为以下几行。
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^index\.php?(.*)$ ./transform.php?p=$1&%{QUERY_STRING} [L]
</IfModule>
问题发生在初始(根)URL:http://mysite.com/。当我输入 http://mysite.com/ 并且网站位于我的本地计算机上时,一切正常。 http://mysite.com/ 被.htaccess捕获并发送到transform.php进行进一步处理。然而,在另一台服务器上,此行不起作用(假设对于 http://mysite-other-server.com /)。此 .htaccess 行根本无法捕获请求。 http://mysite-other-server.com/index.php 不过。
此外,我在第二台服务器上有几个站点,其中一些可以工作,而有些则不能。对于其中一些,URL 首先解析为 http://mysite-other-server。 com/index.php 然后由 .htaccess 处理,而对于其他 URL 则未解析,.htaccess 处理 http://mysite-other-server.com/
我很困惑,尝试谷歌搜索但没有任何运气。非常感谢任何帮助。
I am using mod_rewrite to create SEO URLs. Basically the thing works in the following way:
all requests are caught by .htaccess and redirected to a php script (let's say transform.php). transform.php parses SEO URL and transforms it into a normal URL, then includes index.php, then catches application's (index.php's) output and uses RegEx to convert all normal URLs to SEO URLs.
So the vital step is to make all request go through transform.php. My .htaccess is rather long however I managed to limit it to the following lines.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^index\.php?(.*)$ ./transform.php?p=$1&%{QUERY_STRING} [L]
</IfModule>
The problem happens with the initial (root) URL: http://mysite.com/. When I type http://mysite.com/ and site is located on my local comp, everything works fine. http://mysite.com/ is caught by .htaccess and sent to transform.php for further processing. However on another server this line doesn't work (let's say for http://mysite-other-server.com/). This .htaccess line simply doesn't catch the request. Everything works fine for http://mysite-other-server.com/index.php though.
Moreover I have several sites on this second server and some of them work while some of them don't. It's like for some of them the URL is first resolved to http://mysite-other-server.com/index.php and then handled by .htaccess while for others the URL is not resolved and .htaccess deals with http://mysite-other-server.com/
I am very confused and tried googling without any luck. Any help is highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设 Apache 配置为不接受 .htacces 文件。
您必须使用AllowOverride 设置从httpd 配置启用它。
http://httpd.apache.org/docs/2.0/mod/ core.html#allowoverride
另一种可能性是生产服务器尚未加载 mod_rewrite,请检查 LoadModule 指令。
I assume that Apache is configured not to accept .htacces files.
You must enable it from the httpd configuration with the AllowOverride setting.
http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride
Another possibility is that the production server hasn't loaded mod_rewrite, check the LoadModule directives for that.