Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS not loading after redirect with htaccess rewrite rule

Tags:

I have the following Short-hand for a user profile url

RewriteRule ^(\w+)/?$ ./profile.php?name_of_user=$1

The site is styled with the appropriate css file when i do site.com/name_of_user

but not when i do site.com/name_of_user/

Although the redirect is to the right page...

Help is appreciated!

like image 634
algorithmicCoder Avatar asked Nov 22 '11 01:11

algorithmicCoder


People also ask

How can I redirect and rewrite my urls with an .htaccess file?

Use a 301 redirect . htaccess to point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "example.com" domain.

What is rewrite rule in htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.

How do I create a redirect rule?

Creating a redirect rule Within the Edit Rule page, enter the following: Name: Redirect from blog (This is a unique name for the rule.) Pattern: ^blog/([_0-9a-z-]+)/([0-9]+) (This pattern will match the URL string that starts with "blog" and captures the second and third segments of the URL into back-references.)


2 Answers

the easiest way to fix this is to specify the full path to your CSS file in the <head> of the HTML file.

If your mod_rewrite rule is modifying the path to where your CSS file is, you'd also want to place this before your RewriteRule:

RewriteCond %{REQUEST_FILENAME} !-f

This makes sure that the request doesn't match a file before rewriting it.

like image 76
Steve Lewis Avatar answered Sep 16 '22 14:09

Steve Lewis


I had this same problem and have found the easiest and most practical solution.

<base href="http://www.example.com/" />

It's super clean and easy to use.

like image 29
Query Avatar answered Sep 16 '22 14:09

Query