Forum Moderators: phranque

Message Too Old, No Replies

.htaccess dynamic to static to 500 pages

I rewrtied and redirected 500 pages of my website

         

skerobics

12:20 pm on May 5, 2011 (gmt 0)

10+ Year Member



The following are my site dynamic categories and same page had different versions of urls as follows

v1--->hxxp://www.mysite.com/imagecat.php?catid=1&subcatid=195
v2--->hxxp://www.mysite.com/imagecat.php?subcatid=195

v1-->hxxp://www.mysite.com/imagecat.php?catid=2&subcatid=200
v2-->hxxp://www.mysite.com/imagecat.php?subcatid=200

v1-->hxxp://www.mysite.com/imagecat.php?catid=3&subcatid=171
v2-->hxxp://www.mysite.com/imagecat.php?subcatid=171

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /imagecat\.php\?catid=1&subcatid=195\ HTTP/
RewriteRule ^imagecat\.php$ hxxp://www.mysite.com/jenna-pietersen-wallpapers.html? [R=301,L]
RewriteRule ^jenna-pietersen-wallpapers.html$ /imagecat.php?catid=1&subcatid=195 [L]

I have used the Individual code to redirect and rewrite all v1(version 1) urls.I there any way that i can redirect version 2 urls to version 1 corresponding urls( clean static urls)with single code in htaccess

g1smd

8:29 pm on May 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Add ( ) ? and \ in the right places:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /imagecat\.php\?(catid=1&)?subcatid=195\ HTTP/
RewriteRule ^imagecat\.php$ http://www.example.com/jenna-pietersen-wallpapers.html? [R=301,L]
#
RewriteRule ^jenna-pietersen-wallpapers\.html$ /imagecat.php?catid=1&subcatid=195 [L]

skerobics

7:18 am on May 6, 2011 (gmt 0)

10+ Year Member



Hello g1smd thanks for your reply. I applied your rule but the url was redirected to this url hxxp://www.iana.org/domains/example/

I applied individual rule to all 500 pages and i want to redirect version 2 urls to version 1 static urls.
Appreciates your effort.thanks u

g1smd

7:49 am on May 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Change
example
and
com
to match you own domain name.

skerobics

8:24 am on May 6, 2011 (gmt 0)

10+ Year Member



Thank you its working now.I made a simple mistake.
How can i extend this rule to rewrite my individual image page with signle rule for each category or which ever is the best.

hxxp://www.imageboxoffice.com/image.php?imgid=3683&subcatid=195------------->

hxxp://www.example.com/jenna-pietersen-wallpapers-3683.html

skerobics

8:25 am on May 6, 2011 (gmt 0)

10+ Year Member



which is the bet method to handle dynamic urls when dealing with this kind of websites.give me links if there are any important threads.

g1smd

8:03 pm on May 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The redirect from dynamic to SEF could be done by a PHP script issuing the redirect instead of using multiple rules in .htaccess.

You could do this with a few lines of code near the top of the existing script. It detects the ID number that was requested, pulls the URL title slug from the database, and checks whether the requested URL included parameters. For parameter-based requests the script issues the 301 redirect to the SEF URL.

An alternative would be to add a rewrite at the top of the .htaccess file that detects direct client requests that include parameters, and rewrites the request to a special script that issues the redirect.
The "hook" in .htaccess should be one of the very first rules:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /image\.php\?imgid=([0-9]+)&
RewriteRule ^image\.php$ /my-special-script.php?imgid=%1 [L]


This is one of the rare occasions where a rewrite will be listed in .htaccess BEFORE the redirects. Normally we would want to avoid that, with rewrites AFTER redirects. However, when the PHP script is going to do all the work, the extra rewrite must be right at the beginning of all of the rules.

The chunk of PHP code that fixes the URL and sends the redirect response will look like this:
<?php
$imgid = $GET[imgid];
$validid = //read database and check if this ID is valid.//

# IF the ID is not valid, immediately send headers:
IF ($validid = false)
{HEADER "Status: HTTP/1.1 404 Not Found";}

# IF the ID is valid, construct redirect URL and send headers:
IF ($validid = true)
{
$titleslug = //Read database and get URL title slug for this ID.//
$server_url = $_SERVER['HTTP_HOST'];
IF (preg_match('/^www\./', $server_url) !== true)
{$server_url = "www." . $server_url;}
$new_url = 'http://' . $server_url . '/' . $imgid . '-' . $titleslug;
HEADER "Status: HTTP/1.1 301 Moved Permanently";
HEADER "Location: " . $new_url;
}
?>

The above PHP code is a basic start showing what is needed.

You can do any test and any manipulation you like in the PHP code. It's one of the few occasions when it is better to do the work in PHP, not in .htaccess.

The above code does the redirect from parameter-based URL to SEF URL.

The other part of the system is the rewrite. This rewrite rewrites SEF URL requests to a script. The script checks the title slug is valid for this request and redirects if it is not.

RewriteRule ^[0-9]+-(.*)$ /image.php?imgid=$1&titleslug=$2 [L]


<?php
$imgid = $GET[imgid];
$titleslug = $GET[titleslug];
$validid = //read database and check if this ID is valid.//

# IF the ID is not valid, immediately send headers:
IF ($validid = false)
{HEADER "Status: HTTP/1.1 404 Not Found";}

# IF the ID is valid but the titleslug is not 100% correct for this ID, construct redirect URL and send headers:
$titleslugfromdb = //read database and get the titleslug for this ID.//
IF ($titleslug !== $titleslugfromdb)
{
$server_url = $_SERVER['HTTP_HOST'];
IF (preg_match('/^www\./', $server_url) !== true)
{$server_url = "www." . $server_url;}
$new_url = 'http://' . $server_url . '/' . $imgid . '-' . $titleslug;
HEADER "Status: HTTP/1.1 301 Moved Permanently";
HEADER "Location: " . $new_url;
}
ELSE
{
# IF the ID is valid, and the titleslug is 100% correct for this ID then serve the content
Serve the content as before - i.e. code from your existing script.
}
?>


You'll need to expand this code to read from the database etc, especially the parts marked in //comments// are just a hint as to what code to add.

lucy24

9:21 pm on May 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Aside: Could someone possibly change this thread title from "500" to "five hundred" or alternate wording? I can't be the only one who keeps thinking it's about Apache error messages :(