Multiple-word query strings to friendly URLs

New to mod_rewrite? This is a good place to start.

Multiple-word query strings to friendly URLs

Postby marcy_sss » Tue Sep 01, 2009 2:43 pm

Hi There.

I am trying to use mod_rewrite in an .htaccess file with no success. I do not get any errors, but I do not get any rewrites, either.

In trying to convert my query string URLs to be more SEO friendly, I am having trouble wrapping my head around this. My URLs do not always have the same number of words or characters, and the words are separated by hyphens. I would like these:

http://library-place.com/phase2/library-place.php
http://library-place.com/phase2/library ... -amenities
http://library-place.com/phase2/downtow ... -dine-play

To become these:

http://library-place.com/phase2/library-place
http://library-place.com/phase2/library ... -amenities
http://library-place.com/phase2/downtow ... -dine-play

(/phase2/ is temporary, how would I add this and remove it later?)

There are 7 php pages I need to rewrite, 4 of which have subpages created via query string. Here is what I have so far:

Code: Select all
Options +FollowSymLinks
RewriteEngine On

RewriteRule ^phase2/library-place/(.*[^/])/?$ /library-place.php?p=$1 [QSA,L]
RewriteRule ^phase2/downtownliving/(.*[^/])/?$ /downtown-living.php?p=$1 [QSA,L]
RewriteRule ^phase2/the-homes/(.*[^/])/?$ /the-homes.php?p=$1 [QSA,L]
RewriteRule ^phase2/skotdal-real-estate /skotdal-real-estate.php [QSA,L]
RewriteRule ^phase2/leasing-details/(.*[^/])/?$ /leasing-details.php?p=$1 [QSA,L]
RewriteRule ^phase2/contact /contact.php [QSA,L]
RewriteRule ^phase2/retail /retail.php [QSA,L]


Thank you very much in advance!
marcy_sss
 
Posts: 3
Joined: Tue Sep 01, 2009 2:32 pm
Location: Seattle, WA

It's a little more complicated than that...

Postby marcy_sss » Tue Sep 01, 2009 3:51 pm

So I forgot to mention there are a few things that really complicate this matter (or so it seems). I understand the issues regarding paths, but I am really confused when it comes to mod_rewrite and all the moving parts on this hybrid Flash/PHP website. My apologies if this is kinda crazy -- the website works great until I try to mod_rewrite!

ISSUE #1:

I created a dynamic Flash component for the navigation that checks the URL for a query string, and compares that to an XML file to get the corresponding "subpage number" in order to highlight the current navitem (input via flashvars). Without $_GET['p'] to check, the Flash movie doesn't know what subpage number it's on.

The question is, how do I check against a URL without $_GET['p']? I sometimes use $_SERVER['PHP_SELF'] or $_SERVER['SCRIPT_FILENAME'], but it doesn't seem as specific and could lead to problems if there are similar page names or strings. For example, the site is at 'library-place.com', and there is a 'library-place' page.

You can see how it works here (i.e. the card catalog):

http://library-place.com/phase2/library ... -amenities

Here is the XML it checks & uses to populate the navigation:

http://library-place.com/phase2/swf/navigation.xml


ISSUE #2:

I am outputting links from the Flash navigation, but they do not get rewritten when I go to a page. Is this a timing issue, or should I have Flash just output the "Friendly URLs"?


ISSUE #3:

I also use a PHP switch on each page that uses $_GET['p']. When I type in in the URLs, my PHP switch quits working because there is no $_GET variable to check. Is this just a timing issue, or do I need to rewrite my switch(es)?

Thank you VERY much!
marcy_sss
 
Posts: 3
Joined: Tue Sep 01, 2009 2:32 pm
Location: Seattle, WA

Postby richardk » Wed Sep 02, 2009 9:32 am

Where are you putting the .htaccess file? Try
Code: Select all
Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteRule ^phase2/(downtownliving|leasing-details|library-place|the-homes)/([^/]+)/?$ /phase2/$1.php?p=$2 [QSA,L]
RewriteRule ^phase2/(contact|retail|skotdal-real-estate)/?$ /phase2/$1.php [QSA,L]

in /.htaccess.

Issues 1 and 3: When it's working $_GET['p'] should exist.

I am outputting links from the Flash navigation, but they do not get rewritten when I go to a page. Is this a timing issue, or should I have Flash just output the "Friendly URLs"?

You should output the friendly URLs.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby marcy_sss » Wed Sep 02, 2009 9:50 am

You are awesome! Thanks so much for your reply.

I am using .htaccess in the 'phase2' directory, so it does not affect the pages with the same names that exist in 'phase 1', i.e. the root folder. Does it need to be on the root?

With your exact rewrite code, I get 404 not found errors when I navigate to the friendly URLs from Flash. For example, this comes up with an error page:

http://library-place.com/phase2/downtow ... ent-future

While it should show this:

http://library-place.com/phase2/downtow ... ent-future

Any suggestions?

Thanks again and have a great day!
marcy_sss
 
Posts: 3
Joined: Tue Sep 01, 2009 2:32 pm
Location: Seattle, WA

Postby richardk » Wed Sep 02, 2009 1:36 pm

I am using .htaccess in the 'phase2' directory

Then replace all
Code: Select all
^phase2/

with
Code: Select all
^


so it does not affect the pages with the same names that exist in 'phase 1', i.e. the root folder.

It would not, as it explicitly refers to the /phase2 directory.

Does it need to be on the root?

The code i provided does, but with the above modification it does not (it has to go in /phase2/.htaccess).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 28 guests

cron