Badr Abdullah ALhinai
Badr Abdullah ALhinai

Reputation: 27

RewriteEngine to hide extensions and GET

How can I rewrite this URL ?

http://website.com/file.php?lang=en to http://website.com/file/en

File name and parameters can change, I found a lot of topics put I did not found the combination of both extensions and GET parameters

Update: I get this to work to hide php extension now how to add the parameters

# Turn mod_rewrite on
RewriteEngine On

# hide .php extension
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]

Upvotes: 0

Views: 509

Answers (2)

anubhava
anubhava

Reputation: 785128

From the code and comment I can say that was one of my answers :P

In order to get this new http://website.com/file.php?lang=en to http://website.com/file/en in place have your .htaccess like this:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/?$ /$1.php?lang=$2 [L,QSA]

# hide .php extension
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Upvotes: 1

Abhishek Prabhakar
Abhishek Prabhakar

Reputation: 41

Try this configuration

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

Upvotes: 0

Related Questions