Solved:Redirecting dir + maintain actual page in directory

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

Solved:Redirecting dir + maintain actual page in directory

Postby fishburn » Tue Oct 13, 2009 8:21 pm

I've been beating my head against the wall with this one for a couple of days (in my spare time, so it's not completely horrible/lame -- but pretty darn close):

I have a site that currently has:
Code: Select all
/dir_one (w/index.php)
/dir_two (w/index.php)
/dir_two/some_page.php?a=x (x is an integer)
/dir_two/other_page.php


Rather than have a ton of pages, I'm moving everything to a db where I store a path and content. So in development, I have one index.php in the root. But I have a page in a subdirectory with good google ranks for certain query string values that I want to handle gracefully/efficiently. So I kept that page in place and use its PHP to redirect to a more friendly URL. While I end up with friendly URLs (e.g. /dir_two/page/item_name), all things go through index.php in the background. So I want
Code: Select all
/dir_one                ==> index.php?page=dir_one
/dir_two                ==> index.php?page=dir_two
/dir_two/page_a.php?a=1 ==> index.php?page=dir_two/page/item_name
/dir_two/page_a.php?a=2 ==> index.php?page=dir_two/page/diff_item
... (and so on for page_a.php?a=x)
/dir_two/page_b.php ==> index.php?page=dir_two/page_b


This is what I in /.htaccess on my dev server:
Code: Select all
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^$ /index.php?p=home
RewriteRule ^([a-z0-9/-]+)$ /index.php?p=$1


/dir_two - by itself - is the problem child. I have the following behavior:
- navigating to "dir_two" sends me to "dir_two/?page=dir_two"
- navigating to "dir_two/" keeps me at "dir_two/" but with a trailing slash unlike every other page.

I've tried adding
Code: Select all
RewriteCond ^%{REQUEST_URI}/$ -d
RewriteRule .* /index.php?p=dir_two [L]


at the end to trim the problem slash specifically for dir_two, but that doesn't work. I still end up "/dir_two/?page=dir_two" ...

Help (solution or suggestion for better handling this new layout + the page I sort of need to keep in place for a bit) is greatly appreciated!

Best regards -
- fishburn
Last edited by fishburn on Wed Oct 14, 2009 12:19 am, edited 1 time in total.
fishburn
 
Posts: 2
Joined: Tue Oct 13, 2009 7:26 pm

Solved

Postby fishburn » Wed Oct 14, 2009 12:17 am

Ok, so I kept digging and kept fighting and kept rewriting my .htaccess file. Here's what I came up with:
Code: Select all
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# basic home page
RewriteRule ^$ index.php?p=home

# friendly urls will get passed to the index page which will grab db content
RewriteRule ^([a-z0-9/-]+)$ index.php?p=$1

# this removes .php from previously-existing .php files
# so /dir_one/specific_page.php => /dir_one/specific_page
RewriteRule ^([a-z0-9]+)/([a-z0-9/-]+)\.php$ /$1/$2 [R=301]

# this removes trailing slashes
# so /dir_two/sub_dir/ => /dir_two/sub_dir
RewriteRule ^(.+)/$ index.php?p=$1 [L]

# this handles a specific google-ranked page that uses QUERY_STRING stuff to point to specific content
# if the query string has q={digit} then first go to some_page.php
RewriteCond %{QUERY_STRING} ^q=(\d)$
# some_page.php?q=\d does the necessary redirects to friendly URLs
# like /dir_two/page/item_name
RewriteRule .* some_page.php [L]


It might not be pretty, but I think it works well enough for my purposes. I was able to get rid of my subdirectory and put a specific page in the root to handle the one page with query string things -- that redirects to a proper friendly URL, and I don't have to fight any longer with /dir_two/.

Still, if someone has a better method, or sees a potential problem with this method, please say so.

Thanks and best regards -
- fishburn
fishburn
 
Posts: 2
Joined: Tue Oct 13, 2009 7:26 pm

Postby richardk » Wed Oct 14, 2009 1:59 am

Just a little bit of cleaning up.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Match q= anywhere in the query string.
RewriteCond %{QUERY_STRING} ^(.*&)?q=\d+(&.*)?$ [NC]
RewriteRule .* /some_page.php [QSA,L]

RewriteRule ^([a-z0-9]+/[-a-z0-9/]+)\.php$ /$1 [R=301,L]

RewriteRule ^$ /index.php?p=home [QSA,L]

RewriteRule ^([-a-z0-9/]*[-a-z0-9])/?$ /index.php?p=$1 [QSA,L]
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 29 guests

cron