Cameron
Cameron

Reputation: 65

Styles and files not working after .htaccess rewrite rule

I've just added a simple rewrite rule to my .htaccess file to drop .php from this page http://themeat.in/register.php

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

but now when I go visit that page without the .php (http://themeat.in/register/) all my styles and files have vanished. When I open up the console I see the page name is being treated as a folder.

This is what the file path should be and was before the rewrite, http://themeat.in/css/styles.css

and this is what it is now, http://themeat.in/register/css/styles.css

I guess it's got something to do with the trailing slash within the rewrite but I'm totally stumped at how to fix this problem? I need the .php dropped and I'd like to keep the trailing slash.

Any help would be greatly appreciated.

Many thanks,

//C

Upvotes: 1

Views: 552

Answers (1)

Amit Verma
Amit Verma

Reputation: 41219

This is because of the rewritten urls. when the url is example.com/register/ apache thinks /register/ is a directory and appends that in front of all relative urls.

To solve this, You can add the following base tag in head section of your webpage :

<base href="/">

For more info, see this post : Seo Friendly Url css img js not working

Upvotes: 1

Related Questions