.htaccess RewriteRule 到路径而不更改 URL [英] .htaccess RewriteRule to path without changing URL
问题描述
所以,我有这个问题:
- 位于
http://example.com/
的基本网站 - 第二个网站位于
http://example.com/web2/
- 人们向第二个网站发出各种请求,例如
http://example.com/myWeb/pg1
和http://example.com/web2/pg2
最近,由于其他一些问题,我需要为第二个网站自定义新路径,同时保持第一个网站正常工作.
Recently and due to some other issues I need to have a custom new path for the second website but also keep the first one working.
想法是允许用户通过以下两个地址访问第二个网站:
The ideia is to allow users to access the second website over the two following addresses:
http://example.com/web2/
http://example.com/alternative-url-web2/
文件夹 /web2/
实际上存在于服务器上,但是我如何模拟文件夹 /alternative-url-web2/
并将请求重定向"到 <代码>/web2/?
The folder /web2/
actually exists on the server, but how can I simulate the folder /alternative-url-web2/
and "redirect" the requests to /web2/
?
请注意,我不希望浏览器上的 URL 发生变化,这必须是无声重定向".而且我还确保所有其他请求,如 http://example.com/other
不会被第二个网站重定向.
Please note I don't want the URL on the browser to change, this must be a "silent redirect". And I also make sure that all other requests like http://example.com/other
are not redirected by the second website.
谢谢.
更新:
根据@anubhava,我可以通过添加我的 .htaccess
来简单地解决这个问题:
According to @anubhava I could simply solve this issue by adding in my .htaccess
:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
这可能工作正常,但我注意到以下几点:
This is probably working fine but I noticed the following:
http://ex.com/alternative-url-web2
被重定向到http://ex.com/web2/
(更改浏览器 URL);http://ex.com/alternative-url-web2/
被重定向到http://ex.com/
(改变浏览器 URL);http://ex.com/alternative-url-web2/someRequest
工作正常,不会更改浏览器 URL;http://ex.com/alternative-url-web2/index.php
工作正常并且不会更改浏览器 URL;
http://ex.com/alternative-url-web2
is redirected tohttp://ex.com/web2/
(changing browser URL);http://ex.com/alternative-url-web2/
is redirected tohttp://ex.com/
(changing browser URL);http://ex.com/alternative-url-web2/someRequest
works fine and does NOT change the browser URL;http://ex.com/alternative-url-web2/index.php
works fine and does NOT change the browser URL;
网站注意事项:
在 /web2/
有一个 .htaccess
,可能 导致上面的有线重定向行为......所以这里是文件内容:
At /web2/
there's an .htaccess
that might be cause the wired redirect behavior above... So here is the file contents:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
index.php
的内部 RewriteRule 会导致这一切吗?如果是,我该如何解决?
Can the internal RewriteRule to index.php
be causing all this? If yes, how can I fix it?
推荐答案
通过 httpd.conf
启用 mod_rewrite 和 .htaccess 然后把这段代码放到你的 .htaccess
下DOCUMENT_ROOT
目录:
Enable mod_rewrite and .htaccess through httpd.conf
and then put this code in your .htaccess
under DOCUMENT_ROOT
directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
替代代码:
RewriteRule ^alternative-url-web2/?$ /web2/ [L,NC]
RewriteRule ^alternative-url-web2/(.+)$ /web2/$1 [L,NC]
这篇关于.htaccess RewriteRule 到路径而不更改 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!