url redirection issue using .htaccess - php

Below are the things i need to convert using .htaccess
If url is sample.xxx.com means i need to convert it to www.xxx.com/domain/sample
I url is sample.xxx.com/category/34/electonics.html means i need to convert it to www.xxx.com/domain/sample/category/34/electronics.html
3.After converting those things again i need to build request to the necessary pages
I have the following rule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/domain/%1/$1 [P,QSA]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)$ index.php?domain=$1 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/$ index.php?domain=$1 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/news/([a-zA-Z0-9_\-]+)/([0-9]+)_([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ news.php?domain=$1&category=$4&news=$3 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/category/([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ category.php?domain=$1&category=$2 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/gallery/([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ gallery.php?domain=$1&category=$2 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/video/([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ video.php?domain=$1&category=$2 [Nc,L]
But it fails
If i change line 4 to
RewriteRule ^(.*)$ http://www.example.com/domain/%1/$1 [NC,QSA]
means it will work but the browser shows the reconstructed url.
Anyone spot my error please.

If you keep http:// in target URI then it will indeed be full redirect.
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|ftp|mail)\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^((?!domain/).*)$ domain/%1/$1 [L,NC]
RewriteRule ^domain/([\w-]+)/news/([\w-]+)/([0-9]+)_([0-9]+)/([\w-]+)\.html$ news.php?domain=$1&category=$4&news=$3 [NC,L]
RewriteRule ^domain/([\w-]+)/category/([0-9]+)/([\w-]+)\.html$ category.php?domain=$1&category=$2 [NC,L]
RewriteRule ^domain/([\w-]+)/gallery/([0-9]+)/([\w-]+)\.html$ gallery.php?domain=$1&category=$2 [NC,L]
RewriteRule ^domain/([\w-]+)/video/([0-9]+)/([\w-]+)\.html$ video.php?domain=$1&category=$2 [NC,L]
RewriteRule ^domain/(.+)/?$ index.php?domain=$1 [NC,L]

Related

.htaccess Rewrite rules causing empty $_GET

I am having a problem where on some of my clean url pages the $_GET values are returning as null.
My URL rewrites:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ /$1 [L,R=301] !facebookexternalhit
RewriteCond %{HTTP_HOST} ^codesmite.com [NC] !facebookexternalhit
RewriteRule ^(.*)$ http://www.codesmite.com/$1 [L,R=301,NC,QSA]
# internally add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,QSA]
RewriteRule ^([0-9]+)/?$ index2.php?section=index&page=$1 [NC,QSA]
RewriteRule ^freebies/?$ index2.php?section=freebies&page=1 [L,NC,QSA]
RewriteRule ^articles/?$ index2.php?section=articles&page=1 [L,NC,QSA]
RewriteRule ^deals/?$ index2.php?section=deals&page=1 [L,QSA,NC]
RewriteRule ^freebies/([0-9]+)/?$ index2.php?section=freebies&page=$1 [NC,L,QSA]
RewriteRule ^articles/([0-9]+)/?$ index2.php?section=articles&page=$1 [NC,L,QSA]
RewriteRule ^deals/([0-9]+)/?$ index2.php?section=deals&page=$1 [NC,L,QSA]
RewriteRule ^article/(.*)$ article.php?stitle=$1 [NC,L,QSA]
RewriteRule ^deal/(.*)$ deal.php?stitle=$1 [NC,L,QSA]
RewriteRule ^freebie/(.*)$ freebie.php?stitle=$1 [NC,L,QSA]
RewriteRule ^legal/?$ legal.php [NC,L,QSA]
The pages I am having problems with are:
www.example.com/articles/
www.example.com/freebies/
www.example.com/deals/
I am trying to set a value on these pages based on $_GET['section'] but $_GET array is null.
There are also folders called "articles", "freebies" and "deals" located in the root directory (I do not know if this makes any difference to my problems).
What am I doing wrong?
Nevermind, as soon as I posted this question I noticed my mistake, forgot to change index2 to index from my test file to live file.
No problem with my code, silly mistake ^_^.

url should rewrite if page and query string is match

I want to redirect page if my url and query string is matched i have try much but no success, can you please told me where i done mistake.
My url is
http://xxx.xxx.xxx.xx/search/videos?search_query=xxx+xxx
and I want to redirect (rewrite using .htaccess) it to
http://xxx.xxx.xxx.xx/search/videos/xxx+xxx
I have tryed
RewriteCond %{QUERY_STRING} ^search_query=([A-Za-z0-9]*)$
RewriteRule ^search/videos$ search/videos/$1 [R=301,L]
RewriteCond %{QUERY_STRING} "search_query=" [NC]
RewriteRule ^(.*)$ search/videos/$1? [L,QSA]
RewriteCond %{QUERY_STRING} ^search_query=$1 [NC]
RewriteRule ^(.*)$ search/videos/$1? [L,QSA]
What i am doing wrong?
You can use the following rule
RewriteEngine on
#terminate the 2nd rewrite iteration
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
# redirect "/search/videos?search_query?foobar" to "/search/videos/foobar"
RewriteCond %{QUERY_STRING} ^search_query=(.+)$
RewriteRule ^search/videos$ /search/videos/%1? [R=301,L]
#internally map "/search/videos/foobar" to "/search/videos?search_query?foobar"
RewriteRule ^search/videos/([^/]+)/?$ /search/videos/?search_query=$1 [NC,L]

mod_rewrite GET form to clean URL

I have tried searching but only came up with this link which I can get to work, just not correctly to apply to my current situation.
RewriteCond %{REQUEST_URI} ^/highscores/personal/$
RewriteCond %{QUERY_STRING} ^user1=(.*)$
RewriteRule ^(.*)$ /highscores/personal/%1? [R=301,L,QSA]
RewriteRule ^highscores/personal/(.*)/vs/(.*) personal.php?user1=$1&user2=$2 [L,QSA]
RewriteRule ^highscores/personal/(.*) personal.php?user1=$1 [L,QSA]
RewriteRule ^highscores/skill/(.*) highscores.php?skill=$1 [L,QSA]
RewriteRule ^highscores/(.*) highscores.php?$1 [L,QSA]
As of right now, it will redirect
http://localhost/highscores/personal/?user1=test
to
http://localhost/highscores/personal/test
like it should.
But I have a compare function which submits a GET request like:
http://localhost/highscores/personal/?user1=test&user2=test2
which needs to come out like
http://localhost/highscores/personal/test/vs/test2
but it comes out like
http://localhost/highscores/personal/test&user2=test2
Edit: Resolved using help from this htaccess tester
RewriteCond %{REQUEST_URI} ^/highscores/personal/$
RewriteCond %{QUERY_STRING} ^user1=([^&]+)&user2=(.*)$
RewriteRule ^(.*)$ /highscores/personal/%1/vs/%2? [R=301,L]
RewriteCond %{REQUEST_URI} ^/highscores/personal/$
RewriteCond %{QUERY_STRING} ^user1=(.*)$
RewriteRule ^(.*)$ /highscores/personal/%1 [R=301,L]
You need to edit your rule
RewriteRule ^highscores/personal/(.*)/vs/(.*) personal.php?user1=$1&user2=$2 [L,QSA]
to
RewriteRule ^highscores/personal/([^/]+)/vs/(.*) personal.php?user1=$1&user2=$2 [L,QSA]
because of greedy regex. You need to break (.*) section if / is found

.htaccess redirecting to 404 error

Problem:
I want to redirect from www.domain.com/index.php?mod=daily to www.domain.com/daily_rent
While redirecting from www.domain.com/advert.php?id=11 to www.domain.com/advert/11 works fine like this:
RewriteCond %{THE_REQUEST} advert\.php\?id=([0-9]+)\ HTTP/
RewriteRule ^advert\.php$ http://www.domain.com/advert/%1? [R=301,L]
the same rule
RewriteCond %{THE_REQUEST} index\.php\?mod=daily\ HTTP/
RewriteRule ^index\.php$ http://www.domain.com/daily_rent? [R=301,L]
redirects to www.domain.com/daily_rent and throws 404 error. What could be the reason?
UPD:
Redirecting from www.domain.com/index.php?mod=daily to www.domain.com/daily_rent works fine, thanks anubhava
In that case another problem, on similar pages i have more than one query params, like this(example):
www.domain.com/?mod=daily&room=1&area=bstn
so, i try use that rule
RewriteCond %{THE_REQUEST} \?mod=daily&room=([0-9]+)&area=([A-Z]+)\ HTTP/
RewriteRule ^$ http://www.domain.com/daily_rent/room/%1/area/%2? [R=301,L]
RewriteRule ^daily_rent/room/([0-9]+)/area/([A-Z]+)/?$ /?mod=daily&room=$1&area=$2 [QSA,L]
what is the solution for that situation?
Thanks
Because you're missing internal rewrite rules. Try these rules:
RewriteCond %{THE_REQUEST} advert\.php\?id=([0-9]+)\s
RewriteRule ^advert\.php$ /advert/%1? [R=301,L]
RewriteCond %{THE_REQUEST} index\.php\?mod=(daily)\s
RewriteRule ^index\.php$ /%1_rent? [R=301,L]
RewriteRule ^(daily)_rent/?$ /index.php?mod=$1 [QSA,L]
RewriteRule ^advert/([^/]+)/?$ /advert.php?id=$1 [QSA,L]
Surely you want it the other way round - to redirect /daily_rent to /index.php?mod=daily? That would be a more common usage or mod_rewrite.
RewriteRule ^daily_rent$ /index.php?mod=daily [R=301,L]
You don't need the RewriteCond

.htaccess Adding trail slashes

I got this code from somewhere.. and what it basically does is remove the .php extension
RewriteEngine On
RewriteRule ^Start/?$ index.php [NC,L]
RewriteRule ^Info/?$ info.php [NC,L]
RewriteRule ^Gallery/?$ gallery.php [NC,L]
it does it work good, but because of some SEO stuff,, i need to add automatic traling slash at the end..
Now it is sitename.com/Start
But it has to rewrite itself to sitename.com/start/
Any solutions??
You should handle this with a 301 redirect, placing it above the rules that you already have defined:
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
It is also worthwhile to remove the optional trailing slash flag in the rules that you've already defined, so your resulting .htaccess file will look something like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteRule ^Start/$ index.php [NC,L]
RewriteRule ^Info/$ info.php [NC,L]
RewriteRule ^Gallery/$ gallery.php [NC,L]
In order to avoid the first condition rewriting valid files too, you should add in another condition:
RewriteCond %{REQUEST_FILENAME} !-f
Finally, your .htaccess should look like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteRule ^Start/$ index.php [NC,L]
RewriteRule ^Info/$ info.php [NC,L]
RewriteRule ^Gallery/$ gallery.php [NC,L]

Categories