PDA

View Full Version : Mod_Rewrite Question


Jazajay
10-16-2007, 07:36 AM
Ok to get theskateparkdirectory.com/browse/oh/columbus/

try adding this to your .htaccess file.

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^browse/(.*)/(.*)/$ browse.php?state=$1&city=$2

But you would need to create a browse.php file off the main dir for it to work.

the second one
theskateparkdirectory.com/browse/oh/
simply put in the .htaccess file

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^browse/(.*)/(.*)/$ browse.php?state=$1

Test this out on a test page if you are live incase I have added a syntax error by mistake.

If you already have a .htaccess file -

Options +FollowSymLinks
RewriteEngine on

only needs to be turned on/placed in on the first two lines.

your .htaccess file should look like -

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^browse/(.*)/(.*)/$ browse.php?state=$1
RewriteRule ^browse/(.*)/(.*)/$ browse.php?state=$1&city=$2

Any problems let me know

Jaza

Jazajay
10-16-2007, 07:41 AM
Also it would be good to redirect browse.php?state=$1&city=$2 to theskateparkdirectory.com/browse/oh/columbus/
other wise you will have dup. content issues to do that place [R=301,L] after both rewrites
as in -

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^browse/(.*)/(.*)/$ browse.php?state=$1 [R=301,L]
RewriteRule ^browse/(.*)/(.*)/$ browse.php?state=$1&city=$2 [R=301,L]

Let me know any issues you have

Jaza

theskateparkdirectory
10-20-2007, 01:51 AM
Thanks a lot. I did run into some problems however. I had to reorder the rewrite rules you gave me so the one with the most variables are first. I don't know why but it worked when I switched them.


RewriteRule ^browse/(.*)/(.*)/$ browse.php?state=$1&city=$2
RewriteRule ^browse/(.*)/(.*)/$ browse.php?state=$1

I am having some troubles with the redirect however. I do not want to redirect from the static url to the dynamic one. I would like to do the reverse of this. For example if the user is on:

../?action=browse&state=oh

I would like them to be redirected to

../browse/oh/

Thanks for all your help

Jazajay
10-20-2007, 05:18 AM
Ok the way I redirect is on the page I use PHP heres how I would do it, there are others.


if('http://www.theskateparkdirectory.com/'.$_SERVER['REQUEST_URI'] != 'http://www.theskateparkdirectory.com/browse/oh/columbus/')
{
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://http://www.theskateparkdirectory.com/browse/oh/columbus/');
exit();
}


Ok lets walk through the code

if('http://www.theskateparkdirectory.com/'.$_SERVER['REQUEST_URI'] != 'http://www.theskateparkdirectory.com/browse/oh/columbus/')


This says is the url to this page is not http://www.theskateparkdirectory.com/browse/oh/columbus/ do the following.
If the url requested is theskateparkdirectory.com/browse/oh/columbus/
it tests false and the page continues to load.
If the IF tests true the header function kicks in and reloads the page but this time the if statement returns false so the page is loaded.

You need the rewrite in the .htaccess for this to work.

This also needs to be before any output this includes the Doctype. Any problems post again. If the page needs to be dynamic I can give you the code.

Hope this helps.
Jaza