Dheeraj Agrawal
Dheeraj Agrawal

Reputation: 2357

Redirection through htaccess not working

I want to rewrite the URL like below:

URL: http://localhost/my_site/?file_name=sample

This is my URL I want to show it like:

URL: http://localhost/my_site/sample

that means I want to remove file_name parameter and get the parameter value and set it in URL, for setting this I have used below code:

RewriteEngine On
RewriteBase /my_site/
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-l 
RewriteRule ^(.*)$ /?file_name=$1 [QSA,L]

But its not working, when I type http://localhost/my_site/sample its showing me the list of all sites present in my local that means its taking me to http://localhost instead of require page. What wrong I am doing?

Please help, Thanks in advance

Upvotes: 0

Views: 142

Answers (2)

ThinkingMonkey
ThinkingMonkey

Reputation: 12727

Assuming that mysite folder is under DocumentRoot. add this to your

RewriteEngine On
RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

RewriteCond %{REQUEST_URI} !-f 
RewriteCond %{REQUEST_URI} !\.\w+$
RewriteCond %{REQUEST_URI} !/$ 
RewriteRule (.*) /$1/ [R,L]

RewriteCond %{REQUEST_URI} ^/(my_site)/([\w\d-]+)/$ [NC]
RewriteRule ^ %1/?file_name=%2 [L,QSA]

RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} (?:(.*)&)?file_name=([\w\d-]+)(.*) [NC]
RewriteRule ^(my_site) $1/%2/?%1%3 [L,R]

For server side:

RewriteCond %{REQUEST_URI} ^/([\w\d-]+)/$ [NC]
RewriteRule ^ /?file_name=%1 [L,QSA]

RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} (?:(.*)&)?file_name=([\w\d-]+)(.*) [NC]
RewriteRule ^ %2/?%1%3 [L,R]

Upvotes: 1

Shabib
Shabib

Reputation: 1697

try this, if it works:

RewriteEngine On
RewriteBase /my_site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ file_name=/$1 [L]  

caution: untested

Upvotes: 0

Related Questions