RewriteBase depending on URL?

Discuss practical ways rearrange URLs using mod_rewrite.

RewriteBase depending on URL?

Postby jfusco » Tue Feb 03, 2009 3:11 pm

I have a shared hosting account on GoDaddy. Within that account I am running 4 different sites, including one at the root that displays a page of links to the other 3 sites. Everything else is in a subdirectory and has at least one subdomain (setup through the GoDaddy control panel) pointing to it.

I recently setup a CMS for one of the sites and it uses a directory-level .htaccess file:
Code: Select all
### SILVERSTRIPE START ###
RewriteEngine On
RewriteBase /wavs2

RewriteCond %{REQUEST_URI} !(^/wavs2$)|(\.gif)|(\.jpg)|(\.png)|(\.css)|(\.js)|(\.php)$

RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* sapphire/main.php?url=%1&%{QUERY_STRING} [L]
### SILVERSTRIPE END ###


Using this setup, I can get to the site just fine using the direct url of www.unclebubby.com/wavs2. If I try using the subdomain, wav.unclebubby.com, I get a 404 page.

If I change the RewriteBase from /wavs2 to /, the problem reverses (essentially) and the subdomain works but direct entry doesn't.

Is there a way to set the RewriteBase based on what URL the user types into the browser? That way, I could say If they type the direct URL, set the base to /wavs2. Otherwise, if they type the subdomain URL, set the base to /. Or am I stuck with one or the other?

If you have any other suggestions for accomodating either URL, I'm open to it. I've been tearing my brain out over this one for three days.

Thanks,
Joe
jfusco
 
Posts: 4
Joined: Tue Feb 03, 2009 2:54 pm
Location: Stillwater, OK

Postby richardk » Wed Feb 04, 2009 11:39 am

Try it without the RewriteBase at all
Code: Select all
Options +FollowSymLinks

RewriteEngine On

## SILVERSTRIPE START ###
RewriteCond %{REQUEST_URI} !^/wavs2$
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule !^.+\.(gif|jpg|png|css|js|php)$ ./sapphire/main.php?url=%{REQUEST_URI} [QSA,L]
### SILVERSTRIPE END ###
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jfusco » Wed Feb 04, 2009 7:34 pm

Thank you for your reply. I had previously tried removing the RewriteBase completely but I went ahead and tried your code. Direct entry continued to work but the subdomain didn't.

Since I have almost everything directed/advertised through the subdomain (wavs.unclebubby.com), I have decided to take a different approach. I used your code because it seems more efficient but I went ahead and set the RewriteBase to /, which gets the subdomain working. If I can get direct entry working, it's gravy.

There are, however, a couple of other things I would like to do, if possible. Here is what the code currently looks like:
Code: Select all
Options +FollowSymLinks

RewriteEngine On

### SILVERSTRIPE START ###

RewriteBase /

RewriteCond %{REQUEST_URI} !^/wavs2$
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule !^.+\.(gif|jpg|png|css|js|php)$ ./sapphire/main.php?url=%{REQUEST_URI} [QSA,L]
### SILVERSTRIPE END ###


Several of my original URLs point to things like /blazing_saddles.htm or /die_hard_with_a_vengeance.htm. The CMS (SilverStripe) uses dashes instead of underscores and there is no extension on the pages. So the first thing I would like to do is strip off the .htm, change any underscores to dashes, and reflect those changes in what the user sees. Basically a direct translation from /blazing_saddles.htm to /blazing-saddles or /die_hard_with_a_vengeance.htm to /die-hard-with-a-vengeance. If it's too difficult to deal with a variable number of words, I have a 404 page that tells them to check the site index.

Once that's done, if I manage to figure out how to get direct access via www.unclebubby.com/wavs2/ working I'll be thrilled but it's no longer a priority.

Thank you in advance for any assistance. I feel like I'm asking for a lot but this subject befuddles me pretty easily.

-- Joe
jfusco
 
Posts: 4
Joined: Tue Feb 03, 2009 2:54 pm
Location: Stillwater, OK

Postby richardk » Thu Feb 05, 2009 2:32 am

I had previously tried removing the RewriteBase completely but I went ahead and tried your code. Direct entry continued to work but the subdomain didn't.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

## SILVERSTRIPE START ###
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule !^.+\.(gif|jpg|png|css|js|php)$ ./sapphire/main.php?url=%{REQUEST_URI} [QSA,L]
### SILVERSTRIPE END ###


Several of my original URLs point to things like /blazing_saddles.htm or /die_hard_with_a_vengeance.htm. The CMS (SilverStripe) uses dashes instead of underscores and there is no extension on the pages. So the first thing I would like to do is strip off the .htm, change any underscores to dashes, and reflect those changes in what the user sees. Basically a direct translation from /blazing_saddles.htm to /blazing-saddles or /die_hard_with_a_vengeance.htm to /die-hard-with-a-vengeance. If it's too difficult to deal with a variable number of words, I have a 404 page that tells them to check the site index.

Replacing _ with - is difficult with mod_rewrite alone, but you can do it with mod_rewrite and PHP. Add the following rule
Code: Select all
RewriteRule ^(.*_.*)\.htm$ ./sapphire/mr_redirect.php?u=$1 [QSA,L]

after
Code: Select all
RewriteEngine On

then create a PHP file in the same directory as main.php called mr_redirect.php and put the following in it
Code: Select all
<?php

if(getenv('REDIRECT_STATUS') !== false && isset($_GET['u']) && !empty($_GET['u']))
{
  header('Location: http://' . getenv('HTTP_HOST') . '/' . str_replace('_', '-', $_GET['u']), true, 301);
}
else
{
  header('Location: http://' . getenv('HTTP_HOST') . '/', true, 301);
}
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jfusco » Thu Feb 05, 2009 4:47 am

Dropping the RewriteBase and the first condition has the same effect as the previous code. I have a feeling there's something about the setup of either my host or the CMS that's not allowing it to happen. Like I said, it's no longer a priority.

Having to combine with php is more effort than I want to get into. It's an intriguing idea but if something went wrong with it I wouldn't know how to fix it (at least not at this point). I'm sure I can find code (or figure out how) to drop the .htm and that would cover a majority of my pages. For the others, I've already updated a custom 404 to tell them to check the site index.

Thanks again,
Joe
jfusco
 
Posts: 4
Joined: Tue Feb 03, 2009 2:54 pm
Location: Stillwater, OK

Postby richardk » Thu Feb 05, 2009 4:53 am

Dropping the RewriteBase and the first condition has the same effect as the previous code. I have a feeling there's something about the setup of either my host or the CMS that's not allowing it to happen. Like I said, it's no longer a priority.

You could redirect the unclebubby.com/wavs2 requests to wav.unclebubby.com
Code: Select all
Options +FollowSymlinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?unclebubby\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/wavs2/(.*)$ [NC]
RewriteRule .* http://wav.unclebubby.com/%1 [R=301,L]


I'm sure I can find code (or figure out how) to drop the .htm and that would cover a majority of my pages.

Code: Select all
Options +FollowSymlinks

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/(.+)\.htm$ [NC]
RewriteRule ^.+\.htm$ /%1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jfusco » Thu Feb 05, 2009 6:22 am

Hot diggety they both work!
YOU ARE THE MAN

A thousand thanks,
Joe
jfusco
 
Posts: 4
Joined: Tue Feb 03, 2009 2:54 pm
Location: Stillwater, OK


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 22 guests

cron