简体   繁体   中英

How can .htaccess change $_SERVER[REQUEST_URI]?

I've a website at http://ex.com/web2/ this is a real path in my server, but I wanted visitors to be able to access the website also over the URL http://ex.com/web3/ (without changing the URL on the browser), so after looking around (and asking help) I added the following to my .htaccess :

<IfModule mod_rewrite.c>
    Options +FollowSymLinks -MultiViews
    RewriteEngine On
    RewriteBase /
    RewriteRule ^web3/?$ /web2/ [L,NC]
    RewriteRule ^web3/(.+)$ /web2/$1 [L,NC]
</IfModule>

The "silent" redirect that DOES NOT change the browser URL works fine, but in PHP if I print $_SERVER[REQUEST_URI] I get the URL the user placed on the browser, /web3/ instead of /web2/ .

Is there any way .htaccess can also "fake" the path that is sent to the PHP var? (I was told this would be hard or even impossible.)

Thank you.

If you have mod_proxy loaded, you could internally proxy the request. That will change the REQUEST_URI server variable, but it's sort of a round about way to do it. Would be far more efficient if it all happened in the php scripts.

RewriteRule ^web3/?$ /web2/ [L,NC,P]
RewriteRule ^web3/(.+)$ /web2/$1 [L,NC,P]

Just need to add the P flag to the square brackets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM