Noob problem with mod rewrite and relative urls

I have to do something terribly wrong, because, no matter how I try to do this, I can not find the answer :(

So I want to have a url like http: // localhost / BLUEPRINT / list / 857 actually load a perfectly working url like this: http: //localhost/BLUEPRINT/list.php? Lid = 857

I can write a rewrite rule in the .htaccess file, and I can read the cover variable. The problem is that all the paths in list.php are relative. Css, images, javascript etc Therefore, when a SEO-friendly URL loads all of these elements, BLUEPRINT / list / 857 / ... is scanned.

So, for example, this: <img src="images/logo.png" /> is actually something like this when querying the seo-friendly url: <img src="list/857/images/logo.png" />

So what can I do?

I could probably try converting all the paths on the page (s) to a relative root (for example, "/BLUEPRINT/images/logo.png") instead of a relative one. But there are dozens on the page, and even if I do, they won’t work on the real server, because there may be β€œ/ images” instead of β€œ/ BLUEPRINT / images”. Therefore, I could not just upload the files to the actual server.

What are my options? How do all these wonderful scripts exist, for example wordpress, joomla, etc to handle this problem? What the hell am I doing wrong? It drives me crazy!

+6
url seo mod-rewrite
source share
3 answers

Your new URLs do not have the same base prefix. This means that relative URL paths are allowed differently. The images/logo.png link is now allowed /BLUEPRINT/list/images/logo.png instead of /BLUEPRINT/images/logo.png .

To fix this, use absolute URL paths in your links, for example:

 <img src="/BLUEPRINT/images/logo.png" /> 

Or, if you do not want to add the base URL /BLUEPRINT/ to all of your link elements, set the base URL with base :

 <base href="/BLUEPRINT/" /> 

Relative URL paths are now resolved from this base URL instead of the current URL. But note that changing the base URL affects all relative URLs.

+3
source share

You can write a condition that allows requests for CSS, JS, and other paths to go over and ignore your rewrite rules for list.php. Here is an example.

 # Turn on URL rewriting RewriteEngine On # If your URL is www.example.com/, use / RewriteBase / # Find CSS and JS paths RewriteCond $1 ^(BLUEPRINT/css|BLUEPRINT/js) # No rewriting RewriteRule ^(.*)$ - [PT,L] 

Here is a great Apache tutorial with many examples: http://httpd.apache.org/docs/2.2/misc/rewriteguide.html

Good luck

EDIT: Another good Apache link is the mod_rewrite docs. http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html Do a text search for 'passthrough'.

Another part of the RewriteRule that needs to be noted is the dash '-'. This means that "no replacement should be made."

Finally, I added the RewriteEngine and RewriteBase rules for example. This should make it more complete.

+1
source share

Have you tried applying a "RewriteBase" -Rule?

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritebase

0
source share

All Articles