的.htaccess重写规则到路径不改变网址 [英] .htaccess RewriteRule to path without changing URL
问题描述
所以,我这个问题:
- 位于
http://example.com/
基地网站
- 位于
http://example.com/web2/
第二个网站 - 在人们作出各种请求的第二个网站一样
http://example.com/myWeb/pg1
和http://example.com/web2/pg2
- Base Website located at
http://example.com/
- Second Website located at
http://example.com/web2/
- People making various requests to the second website like
http://example.com/myWeb/pg1
andhttp://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.
的IDEIA是允许用户访问第二网站在两个以下地址:
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/
http://example.com/web2/
http://example.com/alternative-url-web2/
文件夹 / WEB2 /
实际上存在于服务器上,但我怎么能模拟文件夹 /替代-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/
(将浏览器的网址); -
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
是造成这一切?如果是的话,我该如何解决?
Can the internal RewriteRule to index.php
be causing all this? If yes, how can I fix it?
推荐答案
到的httpd.conf
启用了mod_rewrite和.htaccess,然后把这个code在< $ C C>的.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]
替代code:
RewriteRule ^alternative-url-web2/?$ /web2/ [L,NC]
RewriteRule ^alternative-url-web2/(.+)$ /web2/$1 [L,NC]
这篇关于的.htaccess重写规则到路径不改变网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!