Aarij Siddiqui
Aarij Siddiqui

Reputation: 383

Why is mod_rewrite adding var/www/html to the resulting url

I am trying to make my service backward compatible, since I have moved the service to a new path internaly, I still want the users to access it with the old url as not all of them have knowledge of this change.

I can accomplish what I want if I add [R] flag at the end of my rewrite rule but it redirects the url on the client side, which I don't want.

My rewrite code:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^/old-path(.*)$ /new-path/$ [L]
</IfModule>

Although this rule results in the following url:

/var/www/html/new-path

Sample request looks something like:

https://host-name/old-path/param1/param2/param3/param4

and rewrite rule should just replace old-path with the new-path.

Can anyone give me some clues about what am I doing wrong? and how can I fix it?

Thanks in advance!

Upvotes: 1

Views: 1726

Answers (1)

Hello Fishy
Hello Fishy

Reputation: 739

If your are aiming for 'hiding' the rewrite, try this:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^/old-path/(.*)$ /new-path/$1 [P,L]
</IfModule>

And if you REALLY want to extract the hostname:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} (.*)
  RewriteRule ^/old-path/(.*)$ http://%1/new-path/$1 [P,L]
</IfModule>

Since %1 references the first bracket of RewriteCond-line... If you want customers to be rewritten to the new URI and see that:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^/old-path/(.*)$ https://host-name/new-path/$1 [R=301,L]
</IfModule>

or in short, and without even using a rewrite:

RedirectMatch ^/old-path/(.*)$ /new-path/$1

To avoid 'https://host-name/' change your DocumentRoot to the parent-directory of 'old-path' and especially 'new-path'.

Upvotes: 3

Related Questions