mydomain.com/alias/ (with trailing slash) breaks image paths

New to mod_rewrite? This is a good place to start.

mydomain.com/alias/ (with trailing slash) breaks image paths

Postby emkayIII » Fri Jan 28, 2005 1:09 am

Hi Bob,

Im using the code you suggested to redirect www.mydomain.com/alias to www.mydomain.com/start.php?a=xyz. This works great, until users type in a trailing slash (www.mydomain.com/alias/) in which case the image URLs break.

images should load from:

www.mydomain.com/img/image1.gif

with the trailing slash they load from:

www.mydomain.com/alias/img/image.gif


My current code:
Code: Select all
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} /data/ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.* - [QSA,L]

RewriteRule ^([a-z0-9]+)/? start.php?a=$1 [QSA,L]


The following is usually suggested, but wont work in my case:

Code: Select all
RewriteRule ^([^/]+)/([^/]*)/?$ /start.php?a=$1 [L]


Thanks for your help.

Mat

PS. Out of interest, do I really need the QSA to retain the QueryString?
emkayIII
 

Postby Caterham » Fri Jan 28, 2005 7:30 am

Hi Mat,

this time the browser causes the problem. You are using relative image paths such as
<img src="socrates/images/logo.gif"
.

For a request like /2323 the browser believes, that the page is in the root directory, so images would be requested from /socrates/images/logo.gif.
But if you are in /2323/, the the browser believes, you're in the directory 2323, so the image request (for the relative path) would be /2323/socrates/images/logo.gif

Two possible solutions:

One is that you change all links to an absolute path like
<img src="/socrates/images/logo.gif"

or you use mod_rewrite to rewrite broken image paths

Code: Select all
RewriteRule ^[a-z0-9]+/img/(.+)$ img/$1 [L]


The following is usually suggested, but wont work in my case:
Code: Select all
RewriteRule ^([^/]+)/([^/]*)/?$ /start.php?a=$1 [L]
This rule is to rewrite to start.php, but in this case we'd like to rewrite images to images, not to start.php. There's no problem with start.php, it's displayed for /2323/ requests.


Out of interest, do I really need the QSA to retain the QueryString?
Only, if your rewrite rule specifies an other query string (qs) (a=$1 in this case). If you don't use the QSA-Flag, the old qs would be ereased.

Robert
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

thanks!

Postby emkayIII » Fri Apr 08, 2005 4:20 am

Robert,

I only had a chance to look at this thread again now, and wanted to say thanks a lot for the detailed answers.

Thank you!

Mat
emkayIII
 


Return to Beginner's Corner

Who is online

Users browsing this forum: Google [Bot] and 89 guests

cron