Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
746 views
in Technique[技术] by (71.8m points)

regex - .htaccess RewriteRule to path without changing URL

So, I've this problem:

  1. Base Website located at http://example.com/
  2. Second Website located at http://example.com/web2/
  3. People making various requests to the second website like http://example.com/myWeb/pg1 and 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/

The folder /web2/ actually exists on the server, but how can I simulate the folder /alternative-url-web2/ and "redirect" the requests to /web2/?

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.

Thank you.


Update:

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 is redirected to http://ex.com/web2/ (changing browser URL);
  • http://ex.com/alternative-url-web2/ is redirected to http://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;

Site Note:

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>

Can the internal RewriteRule to index.php be causing all this? If yes, how can I fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a pretty simple rewrite. In the htaccess file in your document root, just add the following:

RewriteEngine On
RewriteRule ^alternative-url-web2/?(.*)$ /web2/$1 [L]

Unlike a redirect, which makes the browser/client send a new request for a new URL (thus changing what's in the browser's location bar), a rewrite happens entirely on the server's side.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...