Warm tip: This article is reproduced from stackoverflow.com, please click
.htaccess php mod-rewrite

URL rewrite in htaccess results is missing CSS and images

发布于 2020-04-09 22:51:09

I need to remove a specific thing from my URL. Here I give a page name user.php and get it using $_GET['url'] then make it array using PHP explode() method.

https://www.example.com/index.php?url=user

I need to remove the index.php?url= like this,

https://www.example.com/user

Now I already use this code in .htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule index.php?url=$1 [QSA, L]

Almost everything was fine the user.php page was loaded perfectly. But when I was given a slash to my URL it stops showing CSS or other IMG directories. But when I delete the .htaccess file it works fine in this URL:

https://www.example.com/index.php?url=user/anonymous

But not works in this URL when I assign the .htaccess rules.

https://www.example.com/user/anonymous

Why my all stylesheet and other directory is not working perfectly?

Questioner
Md. Tahazzot
Viewed
77
MrWhite 2020-02-02 02:43
RewriteRule index.php?url=$1 [QSA, L]

I think you have a couple of glaring typos(?) in your question that make this directive completely invalid... you are missing a RewriteRule pattern, so this won't actually match anything and the erroneous space in the flags argument is syntactically invalid, resulting in a 500 Internal Server Error response?!

The RewriteRule directive should be written like:

RewriteRule ^([\w/-]+)$ index.php?url=$1 [QSA,L]

^([\w/-]+)$ matches a URL-path containing any of the characters: a-z, A-Z, 0-9, _ (underscore), / (slash) and - (hyphen). I've excluded the dot, which is naturally part of real filenames.

You should also ensure that MultiViews is disabled, so that mod_negotiation doesn't rewrite the request before mod_rwrite - since your "extensionless" requests appear to map directly to filenames. eg. /user maps to /user.php. Place the following at the top of your .htaccess file:

Options -MultiViews

But when I was given a slash to my URL it stops showing CSS or other IMG directories.

This isn't a problem with .htaccess, but is caused by using relative client-side URLs to your static resources. When you request the URL /user/anonymous then the browser will resolve any relative URLs relative to /user/anonymous (not the document root - which is probably what you are expecting). If you have a relative URL to css/styles.css then the browser is naturally going to resolve this to /user/css/styles.css - which probably doesn't exist (and is likely getting rewritten to index.php - but that isn't the issue - the fact that it doesn't exist is the issue).

If you look at the network traffic (HTTP requests) in the browser, it should give you a clue as to what's going on.

You need to change your client-side URLs to use either root-relative (starting with a slash) or absolute (scheme + hostname) URLs to fix this issue.

See my answer to the following question on the Webmasters stack that explains this further: