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.
Related
I have a script located at http://www.foo.bar/script and would like to use virtual path names to send data to that script. For example, http://www.foo.bar/script/this/is/a/path would pass "this/is/a/path" to /script. I would like to do this without changing the URL the user sees.
I've already gotten this to work with Apache mod_rewrite using something similar to what was suggested here htaccess mod_rewrite multiple paths to query string. What I have not been able to do is to pass the path to the script without changing the URL the users sees. So, a user that visits http://www.foo.bar/script/stackoverflow/rocks, the script residing at /script would receive /stackoverflow/rocks as a query string or URI but the URL would not change. I know this is not uncommon, and perhaps I'm using the wrong terminology when searching for an answer. Thank you for considering.
One idea someone suggested:
RewriteEngine On
RewriteBase /
RewriteRule "script/(.*)" "https://www.foo.bar/script/?data=$1" [R,L]
However, the above changes the URL in the browser. I don't want to expose "?" on the query string.
I don't really have the exact code you need but I have something I use that might just help you. The example above using rewrite_module will rewrite any URL with .php and will also accept the page name without the .php and redirect to a pageName.php witch is close to what you want, the name the users write, redirecting to the script you want. Additionaly it also offer some protection against cross scripting at the end of the URL.
<IfModule rewrite_module>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{QUERY_STRING} ^(.*?)(?:%3C|%3E|<|>)(.*)$
RewriteRule ^((.*)((?!([.]*p*h*p))\w+)) /$1?%1%2 [L,R=302,NE]
</IfModule>
So, I've this problem:
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 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?
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]
Alternate code:
RewriteRule ^alternative-url-web2/?$ /web2/ [L,NC]
RewriteRule ^alternative-url-web2/(.+)$ /web2/$1 [L,NC]
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.
By the way, in order to follow the trail of htaccess redirects, you could add something like this to each of them:
Header add X-Remark-Rewrite "/path.to/htaccess"
You can inspect these in the response in the developer tools.
This could be probably duplicate due to unable to find that original even after checked the similar questions list of stackoverflow. Actually if user type myipaddress/gt then it should redirect to myipaddress/gt/gt.htm
I tried various mod_rewrites but still gets the 'gt' folder contents listed in browser. I gave the last try with below mod_rewrite.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^gt$ gt/gt.htm [NC,L]
But still the browser shows the contents of the 'gt' folder. What am I doing wrong?
You can use this rule in /gt/.htaccess:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /gt/
RewriteRule ^/?$ gt.php [L]
Due to the fact that Apache sees gt as a directory, it treats it as such. However, that doesn't stop all rewriting for that particular path.
This is the basic process:
You navigate to /gt.
Apache sees that as an existing directory, and thus redirects to /gt/.
Your rule is ignored, because it doesn't contain the trailing slash.
This is why you still see the directory listing.
As such, you should change your rule to this:
RewriteRule ^gt/$ gt/gt.php [NC,L]
Alternatively, you can make use of DirectorySlash off and make the trailing slash optional, like so:
Options +FollowSymlinks
DirectorySlash off
RewriteEngine On
RewriteBase /
RewriteRule ^gt/?$ gt/gt.php [NC,L]
Doing either of the above allows you to have multiple rules of a similar nature in a single .htaccess file.
Background
I have a CakePHP application that lives in /m/. I want to write a root-level .htaccess file which will redirect "subdomains" for the site as parameters to actions.
For example: I want to write a rewrite rule which will result in redirects like this -
http://mysite.myserver.com → http://myserver.com/m/mysite/
http://mysite.myserver.com/home → http://myserver.com/m/mysite/home
http://mysite.myserver.com/foo/bar?baz=true → http://myserver.com/m/mysite/foo/bar?baz=true
Ideally, this redirect should be invisible to users (I don't want to use a 301, because I don't want to change the URL).
Here's my current attempt:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.myserver\.com$ [NC]
RewriteRule ^(.*)$ http://myserver.com/m/%1/$1 [L]
</IfModule>
As far as I can tell, the main issue is with $_SERVER['REQUEST_URI']:
If I browse to http://myserver.com/m/mysite/home directly, $_SERVER['REQUEST_URI'] = /m/mysite/home.
If I browse to http://mysite.myserver.com/home using the .htaccess file above, $_SERVER['REQUEST_URI'] = /home.
The Issue
Because of the issues with $_SERVER['REQUEST_URI'], my routes aren't parsing correctly: it's trying to take the user to /home rather than /m/mysite/home as desired.
How can I change my rewrite rule to make this work properly? Is there another way to accomplish this?
What you're asking (change the domain name in URL but don't let browser see URL change) is not achievable under normal scenario. However to make it possible you have enable mod_proxy in your Apache and restart it. Once that is done use following code in your site root .htaccess:
RewriteCond %{HTTP_HOST} ^([^.]+)\.myserver\.com$ [NC]
RewriteRule !^m/ http://myserver.com/m/sites/%1%{REQUEST_URI} [NC,L,P]
I know that there are many questions about this subject, but the questions, and even more the answers are kind of confusing me.
What I want to do:
I want to have an internet page, wich, depending on the URL, shows different content. However, in the backend, all pages are handled by one central PHP page.
In other words:
www.example.com/
www.example.com/AboutUs
www.example.com/Contact
should all be handled by a single .php script, but in such a way that in the browser of the users the URLS are kept intact.
Now, is this done with .htaccess rewriting or not? And how?
.htaccess using Rewrite would be the best approach for this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>
In your index.php you can use the value of $_GET['uri'] or $_SERVER['REQUEST_URI'] to determine which functionality is being requested.
If you only want your controller script to handle requests for files and directories that don't already exist, you can do:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>
Yes, you can achieve this by adding mod_rewrite rules to your .htaccess file. Here is an article with more detailed information: http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/.
It may not help your confusion, but it will at least teach you the proper syntax. Basically, mod_rewrite takes the "clean" URL given in the browser, decodes it using a regular expression, then discretely passes the matches from the regular expression as GET variables.
In other words: mod_rewrite takes "example.com/AboutUs", reads the URL, and serves up whatever would be on the page "example.com/index.php?page=AboutUs" without showing users the actual GET-variable-ridden URL.