codeGenius
codeGenius

Reputation: 65

RewriteCond Query String .htaccess

EDIT: I must add something.First of all i want to change url display because of SEO.If i use www for reach my website there is no problem, second link is appears and everything is ok.

But if i remove "www" from link, it changing to first url and i doesn't want that.

I want to change

http://www.mysite.com/index.php?route=epson-claria-uyumlu-yazici-kartus-dolum-murekkebi-500g.html

to

http://www.mysite.com/epson-claria-uyumlu-yazici-kartus-dolum-murekkebi-500g.html

how I can do it?

I tried

RewriteCond %{QUERY_STRING} ^_route_=(.*)$
RewriteRule ^index\.php$ /%1 [R=301,L]

but it is not working.

My .htaccess is

RewriteBase /

RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]

RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

RewriteCond %{QUERY_STRING} ^route=common/home$
RewriteRule ^index\.php$ http://www.mysite.com? [R=301,L]

RewriteCond %{HTTP_HOST} !^www\.mysite\.com$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L] 

Upvotes: 0

Views: 997

Answers (2)

inhan
inhan

Reputation: 7470

Initial note: I'm not an Apache guru so don't rely blindly on my answer.

I would first redirect to www. if required

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{SERVER_NAME}/$1 [R=301,L]

Then make it go to the page indicated by the _route_ query variable

# if it is the index page...
RewriteCond %{REQUEST_URI} ^/(index\..+)?$ [NC]
# and if the query string starts with _route_=
RewriteCond %{QUERY_STRING} ^_route_=(.*)$
# redirect
RewriteRule ^(.*)$ http://%{SERVER_NAME}/%1? [R=301,L]

Server variable SERVER_NAME in last line might need to be changed with HTTP_HOST.

Upvotes: 1

BenM
BenM

Reputation: 53198

Maybe this is what you're looking for:

RewriteRule ^(.*)$ index.php?route=$1 [L]

If you have to visually change the address bar, leave the RewriteRule in place as I described above, and put this in your index.php before any output:

if(isset($_REQUEST['route']))
{
    header('Location: '.urlencode($_REQUEST['route']));
}

Upvotes: 1

Related Questions