Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess: Redirect without changing url

I would like to have a URL be redirected to a different page on the same domain but without the browser changing the URL. So the page www.mydomain.co.uk/tour/ should point towards www.mydomain.co.uk/ but without changing.

I have looked at a lot of similar questions on Stackoverflow but all the solutions seem to change the URL for me.

CODE:

RewriteEngine On Options +FollowSymLinks RewriteCond %{REQUEST_URI} ^/tour RewriteRule ^(.*)$ http://www.mydomain.co.uk/ [L]  
like image 401
Theo Kouzelis Avatar asked Feb 15 '13 13:02

Theo Kouzelis


People also ask

How can I redirect to another page without changing the URL in PHP?

You should use iframe html tag in page layout for the url of the browser remain www.example.com, when user navigates between pages. not redirect nor rewrite are not suitable for these purposes.

How do I redirect my site using a .htaccess file?

Force the use of HTTPS instead of HTTP for your website If you want to force your website to use HTTPS, you need to use the RewriteEngine module in the . htaccess file. First of all, you need to turn on the RewriteEngine module in the . htaccess file and then specify the conditions you want to check.


2 Answers

Because you provide a full URL in your rewrite rule it is automatically treated as a redirection. Replace the full URL with just a slash and it should work, i.e.:

RewriteCond %{REQUEST_URI} ^/tour RewriteRule ^(.*)$ / [P]  

You can even shorten it down to:

RewriteEngine on RewriteRule ^/?tour.* / [P] 
like image 113
Michal M Avatar answered Oct 15 '22 01:10

Michal M


1- Use [P] instead of [L]

2- Use $s at the end of second line to have a set of urls redirects and remove / at the end of it too.

The code will look like this:

RewriteCond %{REQUEST_URI} ^/tour RewriteRule ^(.*)$ /$1 [P]  

which treats with more than the index page of the folder tour.

like image 20
Amir Ghorbani Avatar answered Oct 15 '22 02:10

Amir Ghorbani