devbull
devbull

Reputation: 227

Alter URL with htaccess

I have a .htaccess file inside folder public (root/public)

I would like to achieve the following transformations:

  1. /index.php -> /
  2. index.php?site=siteName -> /siteName/
  3. /siteName/ or /siteName -> serves as ?site=siteName

Here is my complete .htaccess file so far (with case 3 solved):

Allow from all

RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ index.php?current=$1
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?current=$1

Upvotes: 1

Views: 31

Answers (1)

anubhava
anubhava

Reputation: 785186

You can use an additional rules for redirection of old URL to pretty URL and index.php removal:

RewriteEngine On

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php\?current=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]

# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=302,NC,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?current=$1 [L,QSA]

Upvotes: 1

Related Questions