views:

41

answers:

1

I have a url rewrite that looks like this:

RewriteEngine On
RewriteRule ^cancun/tours/$ location-tours-listings.php?locationName=Cancun
RewriteRule ^cancun/tours$ location-tours-listings.php?locationName=Cancun

Problem is that now the page can't find the relative URL's, such as CSS, images, etc...

Is there a way to fix this with a command in the .htaccess file? So that when someone goes to /cancun/tours/ apache knows that it should look relative to the location-tours-listings.php file and not those other directories?

Or is the only alternative to go into the file that it is redirecting to and hard-code all of those files?

+1  A: 

If you want to fix it through .htaccess, you can add the 301 redirect to the .htaccess file in the root folder for each individual site, as in this example:

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^example\.com [NC] 
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

If you prefer fixing this on the browser-side, you can use the <base> tag in the <head> section of your HTML as in the following example:

<head>
    <title>Page Title</title>
    <base href="http://www.example.com" />
</head>
Daniel Vassallo
thanks, never knew the base could do something like this so I always overlooked it. Thank you
krio
@krio: You may want to check this article for some tips on the `<base>` tag: http://www.drostdesigns.com/base-href-tag/
Daniel Vassallo
thanks that helps! Do you have any resources for handling spaces in apache rewrite? I have some get's that have spaces and apache is having a real problem with them
krio
@krio: `RewriteRule ^path/with\ space.php$ /path/with_space.php [R=301,L]` Try escaping your space with a backslash "\" as in this example.
Daniel Vassallo
Note that changing the base URL will affect *all* relative URLs.
Gumbo