Mod Rewrite Help

Discuss practical ways rearrange URLs using mod_rewrite.

Mod Rewrite Help

Postby lostdog » Tue Apr 22, 2008 5:06 pm

I am trying to do a mod rewrite for all 50 States.
(e.g. http://www.mydomain.com/inspectors/inspectors.php?st=md)

rewritten to: (e.g. http://www.mydomain.com/inspectors/maryland.html)

How would I do this for all 50 States with one rule?
the only difference in the url is the state abrevation.

Can it be done?

I have been trying for hours but dont have a clue!

Thanks in advance.
lostdog
 
Posts: 6
Joined: Tue Apr 22, 2008 5:04 pm

Postby gvamsi » Tue Apr 22, 2008 5:32 pm

This can be done using a text file one to one mapping and can be done only in .htconf (.htaccess doesn't work) and the mapping files should be placed in any root folder ...to make sure you are doing right just to put in .htconf folder

when u enter http://www.mydomain.com/inspectors/inspectors.php?st=md
it will show http://www.mydomain.com/inspectors/maryland.html

map1.txt contains

md marryland
tx texas
wa washington
etc..
Code: Select all
RewriteMap map1 txt:/etc/..../map1.txt
RewriteCond %{QUERY_STRING} ^(.*&)?st=([^&]+)(&.*)?$ [NC]
RewriteRule ^/inspectors/inspectors\.php$ /inspectors/${map1:%2}?\.html [R=301,L]

when u enter http://www.mydomain.com/inspectors/maryland.html
it will show the contents from http://www.mydomain.com/inspectors/inspectors.php?st=md but the url will be the same

map2.txt

marryland.html md
texas.html tx
washington.html wa
etc..
Code: Select all
RewriteMap urlm1 txt:/etc/..../map2.txt
RewriteRule ^/inspectors/([^/]+)/?$ /inspectors/inspectors.php?st=${map2:$1} [PT,L]


This should do it
I'm very new to this richardk helped me for my prob. ,so just using the stuff he has given to solve urs.

May be this helps too -- viewtopic.php?t=5651
gvamsi
 
Posts: 11
Joined: Mon Apr 21, 2008 2:44 pm

Postby lostdog » Tue Apr 22, 2008 6:31 pm

gvamsi,

Thanks for your help I am working on it now and will let you know.

What is your opinion for url seo on States like New York?

Whould you use newyork.html or new-york.html?
lostdog
 
Posts: 6
Joined: Tue Apr 22, 2008 5:04 pm

Postby lostdog » Tue Apr 22, 2008 6:55 pm

Not Working, probably my fault, heres what I have done.

I created a 3 files in my folder: /public_html/inspectors

1. .htconf (entered the code's you wrote)

RewriteMap http://www.mydomain.com/inspectors/map1.txt
RewriteCond %{QUERY_STRING} ^(.*&)?st=([^&]+)(&.*)?$ [NC]
RewriteRule ^/inspectors/inspectors\.php$ /inspectors/${map1:%2}?\.html [R=301,L]

RewriteMap http://www.mydomain.com/inspectors/map1.txt
RewriteRule ^/inspectors/([^/]+)/?$ /inspectors/inspectors.php?st=${map2:$1} [PT,L]

2. map1.txt (md maryland etc....) with a return after each entry
3. map2.txt (maryland.html md etc...) with a retun after each entry
lostdog
 
Posts: 6
Joined: Tue Apr 22, 2008 5:04 pm

Postby gvamsi » Tue Apr 22, 2008 7:24 pm

I don't think it will work like this(RewriteMap http://www.mydomain.com/inspectors/map1.txt ) ..... you have to put the mapping files in a root folder
say I use /etc/httpd/conf/map1.txt .......

I suggest to put in .htconf folder ...

I would use newyork.html for SEO

what do u mean with a return after each enty ?
gvamsi
 
Posts: 11
Joined: Mon Apr 21, 2008 2:44 pm

Postby lostdog » Wed Apr 23, 2008 5:18 am

Is the .htconf a folder or a file?

If its a folder I cant place the rewrite info just inside a folder it needs a shell.

I created .htconf file with the rewrite info inside placed it in my root folder. I also placed the two map.txt files in my root folder no luck!

Return is just another line in the text folder.

e.g.
al alabama
ak alaska
az arizona
ar arkansas
ca california
co colorado
ct connecticut

I am missing something:(
lostdog
 
Posts: 6
Joined: Tue Apr 22, 2008 5:04 pm

Postby gvamsi » Wed Apr 23, 2008 9:43 am

I'm sorry for the confusion ... you should see a file with name httpd.conf in your configuration dir.

You should place the above code in httpd.conf file
gvamsi
 
Posts: 11
Joined: Mon Apr 21, 2008 2:44 pm

Postby richardk » Thu Apr 24, 2008 1:39 pm

Where is the .htconf file? If it is equialent to a .htaccess file a RewriteMap cannot be used in it.

There isn't really a short way to do it with only mod_rewrite. You could
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond al$1 ^(al)alabama$ [NC,OR]
RewriteCond ak$1 ^(ak)alaska$ [NC,OR]
RewriteCond az$1 ^(az)arizona$ [NC,OR]
RewriteCond ar$1 ^(ar)arkansas$ [NC,OR]
RewriteCond ca$1 ^(ca)california$ [NC,OR]
RewriteCond co$1 ^(co)colorado$ [NC,OR]
# etc.
RewriteCond ct$1 ^(ct)connecticut$ [NC]
RewriteRule ^inspectors/([^/]+)\.html$ /inspectors/inspectors.php?st=%1 [QSA,L]

This means only one rule, not 50 seperate rules,will be processed if it does not match the /inspectors/*.html format.

I suggest you add an array to your PHP instead (ie. rewrite the full state name and then use PHP to get the 2 letters)
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^inspectors/(alabama|alaska|arizona|arkansas|california|colorado|connecticut)\.html$ /inspectors/inspectors.php?st_long=$1 [QSA,L]

and in inspectors.php
Code: Select all
$st_long = array(
  alabama'     => 'as',
  alaska'      => 'ak',
  arizona'     => 'az',
  arkansas'    => 'ar',
  california'  => 'ca',
  colorado'    => 'co',
  connecticut' => 'ct'
  );

if(isset($_GET['st_long']) && array_key_exists(trim(strtolower($_GET['st_long']))))
{
  $_GET['st'] = $st_long[trim(strtolower($_GET['st_long']))];
}
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 98 guests

cron