SEO defined rewrite

Discuss practical ways rearrange URLs using mod_rewrite.

SEO defined rewrite

Postby mwe » Tue Feb 17, 2009 7:22 am

I have a client that wants to convert their old URL string to something that is more SEO friendly without having to redo their entire website and directory patch structure.

Code: Select all
http://mysite.com/classic/us/ga/atl/0/abc.shtml


to

Code: Select all
http://mysite.com/US/Georgia/Atlanta/Dining/10_Degrees_South


Since there are several hundred files per city code (based on listing type -- i.e. dining, shopping, entertainment, etc), is it possible to have Apache/mod_rewrite do something convert the old URL path (on-the-fly) to the new SEO layout sort of like how 'awk' works?

File structure: /web/mysite.com/htdocs/classic/us/ga/atl/0/abc.shtml

$1 = web
$2 = mysite.com
$3 = htdocs
$4 = classic
$5 = us
$6 = ga
$7 = atl
$8 = 0
$9 = abc.shtml

Variables that are needed...

$5 -- Country Code
$6 -- State Code
$7 -- City Code
$8 -- Listing Type
$9 -- Business Listing Webpage

---

I was thinking I could use the RewriteMap Directive for the full name of the County, State and City codes when identified from the old URL listing and rewrite the URL that way, but it gets a bit confusing...

Original URL: http://mysite.com/classic/us/ga/atl/0/abc.shtml

SEO County Map file: /web/mysite.com/htdocs/seo_country.txt
Code: Select all
us US/
cn Canada/


SEO State Map file: /web/mysite.com/htdocs/seo_state.txt
Code: Select all
al Alabama/
ga Georgia/


SEO City Map file: /web/mysite.com/htdocs/seo_city.txt
Code: Select all
atl Atlanta/
sav Savannah/


SEO Listing Map file: /web/mysite.com/htdocs/seo_listing.txt
Code: Select all
0 Dining/
1 Shopping/
2 Entertainment/
3 Museums/
4 Navigate/


SEO Location Map file: /web/mysite.com/htdocs/seo_location.txt
Code: Select all
abc.shtml 10_Degrees_South/
def.shtml Steaks-R-Us/


Actual mod_rewrite code
Code: Select all
RewriteEngine  On
RewriteMap    country  txt:/web/mysite.com/htdocs/seo_country.txt
RewriteMap    state      txt:/web/mysite.com/htdocs/seo_state.txt
RewriteMap    city        txt:/web/mysite.com/htdocs/seo_city.txt
RewriteMap    listing     txt:/web/mysite.com/htdocs/seo_listing.txt
RewriteMap    location     txt:/web/mysite.com/htdocs/seo_location.txt
RewriteRule   ^/classic/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?(.*)   ${country:$1}/{state:$2}/{city:$3}/{listing:$4}/{location:$5} [redirect=permanent,last]


---

The end run solution would be that the URL to show up as the SEO URL when seen in a browser rather than the drawn out 'old' URL path.

Old URL: http://mysite.com/classic/us/ga/atl/0/abc.shtml
SEO URL: http://mysite.com/US/Georgia/Atlanta/Di ... rees_South

-- Michael
Last edited by mwe on Tue Feb 17, 2009 7:51 am, edited 1 time in total.
mwe
 
Posts: 21
Joined: Fri Aug 10, 2007 7:12 am
Location: North Augusta, SC USA

Postby richardk » Tue Feb 17, 2009 7:46 am

Since there are several hundred files per city code (based on listing type -- i.e. dining, shopping, entertainment, etc), is it possible to have Apache/mod_rewrite do something convert the old URL path (on-the-fly) to the new SEO layout sort of like how 'awk' works?

Do you mean the links in the page source? It won't change them, it works on the incoming request only.

Is /classic/us/ga/atl/0/abc.shtml an actual HTML file? If it is are you going to keep it where it is?

I was thinking I would also need to be using the RewriteMap Directive for the full name of the County, State and City codes when identified from the old URL listing.

Do you have access to the httpd.conf file to define a RewriteMap?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby mwe » Tue Feb 17, 2009 7:56 am

richardk wrote:Is /classic/us/ga/atl/0/abc.shtml an actual HTML file? If it is are you going to keep it where it is?

Do you have access to the httpd.conf file to define a RewriteMap?


1st Question: Yes. The client doesn't want to have to redo their entire environment. They want it to look seamless.

2nd Question: Yes. I control the Apache instances and all virtual hosts as well for the environment.

NOTE: I edited my original question and posted a possible RewriteMap solution, but I think I might be just confusing myself.
mwe
 
Posts: 21
Joined: Fri Aug 10, 2007 7:12 am
Location: North Augusta, SC USA

Postby mwe » Tue Feb 17, 2009 9:16 am

Okay, I found that a mod_rewrite w/ RewriteMap code is doing what I want it to do:

Code: Select all
RewriteEngine  On
   RewriteMap    country  txt:/web/mysite.com/htdocs/TEST/seo_country.txt
   RewriteMap    state      txt:/web/mysite.com/htdocs/TEST/seo_state.txt
   RewriteMap    city        txt:/web/mysite.com/htdocs/TEST/seo_city.txt
   RewriteMap    listing     txt:/web/mysite.com/htdocs/TEST/seo_listing.txt
   RewriteMap    location     txt:/web/mysite.com/htdocs/TEST/seo_location.txt
   RewriteRule   ^/TEST/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?(.*)  ${country:$1}/${state:$2}/${city:$3}/${listing:$4}/${location:$5} [L]


_except_ it is actually attempting to redirect to the new path layout rather than just display it in the browser.

Code: Select all
http://mysite.com/US/Georgia/Atlanta/Dining/10_Degrees_South


What do I need to do to have the following work:
1) Change the browser 'seen' address to an SEO site layout but behind the scenes keep the original URL path.

2) If someone goes to the SEO URL, it keeps the browser address to an SEO site layout but behind the scenes remaps to original URL path.

http://mysite.com/US/Georgia/Atlanta/Di ... rees_South == http://mysite.com/classic/us/ga/atl/0/abc.shtml

-- Michael
mwe
 
Posts: 21
Joined: Fri Aug 10, 2007 7:12 am
Location: North Augusta, SC USA

Postby mwe » Tue Feb 17, 2009 1:01 pm

Upon further research, it seems that mod_rewrite cannot be used to change the URL that the visitor sees in his/her browser address bar unless an external redirect is invoked. Mod_rewrite works on requested URLs after the HTTP request is received by the server, and before any scripts are executed or any content is served. That is, mod_rewrite changes the server file path and script variables associated with a requested URL, but has no effect whatsoever on the content of 'pages' output by the server.

The real problem is that the 'US/Georgia/Atlanta/Dining/10_Degrees_South' portion does not exist on the server-side at all. It is simple for making it easier to read via the browser and for SEO needs. Only the 'TEST/us/ga/atl/0/abc.html' directories and file actually exists.

So I guess my question now is...

1) How does one trigger the 'external redirect' script under Apache?
2) How does one go about doing what I am trying to do w/o the use of mod_proxy?

I have posted a copy of the Virtual Host I am toying with for testing purposes.

Code: Select all
<VirtualHost *>
   ServerName mysite.com
   DocumentRoot /web/mysite.com/htdocs
   ErrorLog /var/web/mysite.com/error_log
   CustomLog /var/web/mysite.com/access_log combined

   RewriteEngine  On
   RewriteMap country  txt:/web/mysite.com/htdocs/TEST/seo_country.txt
   RewriteMap state    txt:/web/mysite.com/htdocs/TEST/seo_state.txt
   RewriteMap city     txt:/web/mysite.com/htdocs/TEST/seo_city.txt
   RewriteMap listing  txt:/web/mysite.com/htdocs/TEST/seo_listing.txt
   RewriteMap location txt:/web/mysite.com/htdocs/TEST/seo_location.txt

   # Incoming URI is seen as http://mysite.com/TEST/us/ga/atl/0/abc.html
   # Run through Rewrite conditioning portion to change to new SEO URL listing instead
   # Example: http://mysite.com/US/Georgia/Atlanta/Dining/10_Degrees_South

   RewriteCond %{THE_REQUEST} ^TEST/${country:$1}/${state:$2}/${city:$3}/${listing:$4}/${location:$5}

   # Now display in browser the SEO URL address (as seen in example)
   # But retrieve data information from original URI address

  # RewriteRule ^/TEST/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?(.*)  ${country:$1}/${state:$2}/${city:$3}/${listing:$4}/${location:$5} [L]
  # Redirect / %{REQUEST_URI}

    RewriteRule ${country:$1}/${state:$2}/${city:$3}/${listing:$4}/${location:$5} %{REQUEST_URI} [L]

   <Directory /web/mysite.com/htdocs>
      Options Indexes FollowSymLinks Multiviews +Includes All
      AllowOverride All
  </Directory>
</VirtualHost>
mwe
 
Posts: 21
Joined: Fri Aug 10, 2007 7:12 am
Location: North Augusta, SC USA

Postby mwe » Tue Feb 17, 2009 5:21 pm

When it comes in question of the static html files themselves, modification to each file is not an option. We are talking about 20 to 30,000 static html files. Not a likely concept for the developers. We need an in-line solution to do the URL masking while allowing the data file themselves to remain untouched.
mwe
 
Posts: 21
Joined: Fri Aug 10, 2007 7:12 am
Location: North Augusta, SC USA

Postby richardk » Wed Feb 18, 2009 10:57 am

1) How does one trigger the 'external redirect' script under Apache?

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^old$ /new [R=301,L]

RewriteRule ^new$ /old [QSA,L]


2) How does one go about doing what I am trying to do w/o the use of mod_proxy?

What do you need mod_proxy for?

modification to each file is not an option.

Then you should look at mod_filter but i bet it's inefficient vs. a mass search and replace script.
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 25 guests

cron