Bilal075
Bilal075

Reputation: 85

Htaccess, redirecting to pretty url

I started looking into Htaccess today and tried it out. What i'm trying to achieve is that whenever you visit localhost/index.php, it'll redirect you to the "pretty" URL (in this case: localhost/home).

This is my Htaccess file at the moment:

RewriteEngine on
RewriteRule ^home/?$ index.php [NC,L]

I know that what i'm currently doing only rewrites /home to /index.php but I couldn't figure out how to do the opposite too..

Help would be appericiated!

Upvotes: 0

Views: 59

Answers (2)

anubhava
anubhava

Reputation: 786031

You need a new redirect rule for that:

RewriteEngine on

RewriteCond %{THE_REQUEST} /(home|index\.php)[?\s] [NC]
RewriteRule ^(index\.php|home)$ / [NC,R=302,L,NE]

RewriteRule ^home/?$ index.php [NC,L]

Upvotes: 0

Mike Rockétt
Mike Rockétt

Reputation: 9007

You can try with the following conditions and rule (commented to explain):

RewriteEngine On

# Check that the request is for `index.php`
# prevent infinite looping:
RewriteCond %{REQUEST_URI} ^/index.php$ [NC]
RewriteCond %{ENV:REDIRECT_STATUS} !200

# If the conditions match, redirect to `/home`:
RewriteRule ^ /home [R=302,L,NE]

# Serve `index.php` for requests made to `/home`
RewriteRule ^home/?$ /index.php [NC,L]

Upvotes: 1

Related Questions