2

Iam trying to rewrite my URL http://givehands.co.in/post.php?p=post&id=1 to http://givehands.co.in/post/1/ using htacess.

But it gets redirected to the URL http://givehands.co.in/post.php?p=post&id=1 instead of rewriting it? My domain givehands is mapped to a sub directory of my webserver.

Could any one help me out with the correct rewrite rule? Iam new to this.

Options -Multiviews
RewriteEngine On    # Turn on the rewriting engine
RewriteBase /
RewriteCond %{REQUEST_URI} post/(.*)
RewriteRule post/(.*)/ http://givehands.co.in/post.php?p=post&id=$1 [L]
RewriteCond %{REQUEST_URI} post/
RewriteRule post/ http://givehands.co.in/post.php [L]
4

1 Answer 1

3

It looks like you have a full URL in your redirect rule:

RewriteCond %{REQUEST_URI} post/ 
RewriteRule post/ http://givehands.co.in/post.php [L]

Putting a full URL in there forces Apache to redirect the browser instead of rewriting it: https://httpd.apache.org/docs/current/mod/mod_rewrite.html

Absolute URL
If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and hostname are stripped out and the resulting path is treated as a URL-path. Otherwise, an external redirect is performed for the given URL. To force an external redirect back to the current host, see the [R] flag below.

If givehands.co.in is the domain you're hosting, try changing it to:

RewriteCond %{REQUEST_URI} post/ 
RewriteRule post/ /post.php [L]

If givehands.co.in isn't on your server, or is under a different VirtualHost, then there's no way to "rewrite" the URL, you have to redirect the browser (which is why Apache is taking the lead and doing this for you).

You could actually setup a reverse proxy to do something like this, but that's probably a topic for a different discussion :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.