Oliver Kucharzewski
Oliver Kucharzewski

Reputation: 2645

Rewrites breaking links

I created a URL rewrite for my blog but all the links such as css, href & javascript are broken due to this.

Without manually continuing to rename all the links by adding a / before each link, would there be anyway to rewrite it within the .htaccess file somehow? Here is what i have:

RewriteRule ^blog/([-a-zA-Z0-9]+)/?$ blog.php?title=$1 [L,NC]

Upvotes: 1

Views: 36

Answers (2)

Jon Lin
Jon Lin

Reputation: 143886

By adding the extra / in your URL, the browser doesn't know what the relative URL base is, so it's appending /blog/ in front of every relative URL link that you have in your page content. The browser doesn't know that it's actually getting content from /blog.php, which sits in the document root, not in what appears (to the browser, at least) to be a folder called "blog".

Instead of changing all of your links, you can add a base URI to the header of your page:

<base href="/" />

Note that in some cases (I think with old IE browsers), you need the full URL:

<base href="http://your-site.com/" />

Upvotes: 1

zessx
zessx

Reputation: 68790

With mod_rewrite, you can check if your URL point on a file :

RewriteCond %{REQUEST_FILENAME} -f

Or a folder :

RewriteCond %{REQUEST_FILENAME} -d

Here's the .htaccess you may use :

# if this url doesn't point on a file...
RewriteCond %{REQUEST_FILENAME} !-f
# ...and doesn't point on a folder...
RewriteCond %{REQUEST_FILENAME} !-d
# ...then check this rule
RewriteRule ^blog/([-a-zA-Z0-9]+)/?$ blog.php?title=$1 [L,NC]

Upvotes: 0

Related Questions