/index.php?page=help to /help.html

Discuss practical ways rearrange URLs using mod_rewrite.

Postby adddxb » Fri Oct 02, 2009 4:06 pm

how i can redirect this by 301

/index.php?page=help
to
/help.html

currently i use this code
RewriteRule ^help.html index.php?page=help [L]

i try to use this code but not work
RewriteRule ^index.php?page=help http://www.mydomain.com/index.html [R=301,L]

Thanx for help
adddxb
 
Posts: 6
Joined: Fri Oct 02, 2009 4:00 pm

Postby DanielStutzbach » Fri Oct 02, 2009 7:07 pm

adddxb wrote:/index.php?page=help
to
/help.html


The RewriteRule doesn't consider the query string part of the URL. You have to use a RewriteCond to handle that.

I believe this will work:

Code: Select all
RewriteCond %{QUERY_STRING} =page=help
RewriteRule ^/index.php$ http://www.mydomain.com/help.html? [L,NC,R=301]
DanielStutzbach
 
Posts: 6
Joined: Wed Aug 19, 2009 6:49 am

Postby richardk » Sat Oct 03, 2009 8:14 am

Nearly
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Don't match internal sub requests.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# Match the query string variable.
RewriteCond %{QUERY_STRING} ^(.*&)?page=help(&.*)?$ [NC]
# Match the file and redirect.
RewriteRule ^index\.php$ /help.html? [NC,R=301,L]

RewriteRule ^help\.html$ /index.php?page=help [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby adddxb » Sun Oct 04, 2009 3:58 pm

richardk wrote:Nearly
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Don't match internal sub requests.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# Match the query string variable.
RewriteCond %{QUERY_STRING} ^(.*&)?page=help(&.*)?$ [NC]
# Match the file and redirect.
RewriteRule ^index\.php$ /help.html? [NC,R=301,L]

RewriteRule ^help\.html$ /index.php?page=help [QSA,L]


Thanks richardk,

it's work fine now but what abt if i have more pages need to redirect it?
like:

index.php?page=rss
index.php?page=help
index.php?page=about

Thanks again
adddxb
 
Posts: 6
Joined: Fri Oct 02, 2009 4:00 pm

Postby richardk » Mon Oct 05, 2009 8:09 am

You can match many values instead of just help.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Don't match internal sub requests.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# Match the query string variable.
RewriteCond %{QUERY_STRING} ^(.*&)?page=(about|help|rss)(&.*)?$ [NC]
# Match the file and redirect.
RewriteRule ^(index\.php)?$ /%1.html? [NC,R=301,L]

RewriteRule ^(about|help|rss)\.html$ /index.php?page=$1 [QSA,L]

You could also match all vales (ie. a wildcard).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby adddxb » Mon Oct 05, 2009 5:49 pm

it's not work fine, links redirect to http://www.mydomain.com/.html


Thanks for help
adddxb
 
Posts: 6
Joined: Fri Oct 02, 2009 4:00 pm

Postby richardk » Tue Oct 06, 2009 10:24 am

%1 should be %2.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby adddxb » Wed Oct 07, 2009 12:03 pm

thanks richardk
all going fine now :)
adddxb
 
Posts: 6
Joined: Fri Oct 02, 2009 4:00 pm

Postby adddxb » Wed Oct 07, 2009 12:53 pm

1 thing also i need to rewrite & redirect it to html

/index.php?page=videos&section=view&vid_id=$

to

/video/$.html


$= video ID
thanks
adddxb
 
Posts: 6
Joined: Fri Oct 02, 2009 4:00 pm

Postby richardk » Wed Oct 07, 2009 2:08 pm

Where's your attempt?

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?page=(about|help|rss)(&.*)?$ [NC]
RewriteRule ^(index\.php)?$ /%2.html? [NC,R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?page=videos(&.*)?$ [NC]
RewriteCond %{QUERY_STRING} ^(.*&)?section=view(&.*)?$ [NC]
RewriteCond %{QUERY_STRING} ^(.*&)?vid_id=([0-9]+)(&.*)?$ [NC]
RewriteRule ^(index\.php)?$ /video/%2.html? [NC,R=301,L]

RewriteRule ^(about|help|rss)\.html$ /index.php?page=$1 [QSA,L]
RewriteRule ^video/([0-9]+)\.html$ /index.php?page=videos&section=view&vid_id=$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Next

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron