bia.migueis
bia.migueis

Reputation: 2016

redirect without changing url

I always suck at .htaccess I'm trying to have a website where all requests go though index.php but i'd like the URL to be like www.sample.com/home. effectively that url would load www.sample.com/index.php?page=home

Right now I have

 RewriteRule ^/([a-z0-9-]+) /index.php?page=$1 [NC,PT,L]

Which doesnt work even though regexr test shows it should. I temporarily changed it to r=301 so i could see the redirect and noticed that www.sample.com/home is redirecting to www.sample.com/index.php?page=/index.php :(

Any thoughts?

Note I've been searching for this for a while and I can't find a solution that actually works. It's not like this was my first stop.

Upvotes: 2

Views: 1910

Answers (1)

anubhava
anubhava

Reputation: 784908

Change your rule to:

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]

RewriteRule ^([a-z0-9-]+)/?$ /index.php?page=$1 [QSA,L,NC]

Reference: Apache mod_rewrite Introduction

Upvotes: 3

Related Questions