Forum Moderators: phranque

Message Too Old, No Replies

Seemingly simple rewrite query (folder related)

Query about (I think fairly basic) redirecting

         

stevej444

8:18 pm on May 20, 2007 (gmt 0)

10+ Year Member



Hi,
Wonder if someone can help. I think this query is pretty basic, but I could be wrong. I have a .htaccess file as follows:

[START of HTACCESS]
RewriteEngine On

# Prevent looping for files with an extension
RewriteCond %{REQUEST_URI}!^/.+\.[a-z5]{2,4}$

# dept redirect
RewriteRule ^([a-zA-Z0-9]+)$ dept.php?dn=$1 [L]

# employee redirect
RewriteRule ^([a-zA-Z0-9]+)-([a-zA-Z0-9]+)$ emp.php?dn=$1&en=$2 [L]

[END of HTACCESS]

Now the above works fine. URLs such as:

[mydomain.com...]
[mydomain.com...]

all redirect fine. As does
[mydomain.com...]

Lovely. However, a URL such as

[mydomain.com...] - redirects to the PHP file no problem, but the CSS file is not included. It appears that the extra "/" on the end has somehow mangled the path to the CSS .... the CSS is included using '<style type="text/css" media="screen">@import url(css/main.css);</style>'

I have tried:

RewriteRule ^([a-zA-Z0-9]+)$ dept.php?dn=$1 [L]
but makes no difference.

Once I get this working, I'd like to move to separators using "/" as opposed to "-" as used currently.

Any suggestions anyone as to how to deal with this folder issue?

Thanks,
S

jdMorgan

8:36 pm on May 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The browser will resolve the page-relative URL reference differently in the trailing-slash versus no-slash cases.

I suggest using server-relative links:


<style type="text/css" media="screen">@import url[b](/c[/b]ss/main.css);</style>'

An alternative is to explicitly rewrite the incorrect css, image, and external JS URLs when received.

Jim

stevej444

9:46 am on May 21, 2007 (gmt 0)

10+ Year Member



Hi Jim,
Thanks as ever.

So what I now have is .....

RewriteRule ^([a-zA-Z0-9]+)/$ dept.php?dname=$1 [L]
RewriteRule ^([a-zA-Z0-9]+)/css/screen.css$ ./css/main.css [L]
RewriteRule ^([a-zA-Z0-9]+)/scripts/js.js$ ./scripts/js.js [L]

However - what I'd like to do is *force* a slash on the end of any dept in a location that doesn't have one e.g.
RewriteRule ^([a-zA-Z0-9]+)$ $1/ [R,L]

but this doesn't seem to quite work. What happens, is, if my URL is [127.0.0.1:83...]

I end up with

[127.0.0.1:83...]
somehow, the name of the web dev folder appears i.e. "web".

Any thoughts on that one?

jdMorgan

2:57 pm on May 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That may be because the syntax isn't quite right:

RewriteRule ^([a-zA-Z0-9]+)$ [b]http://www.example.com/[/b]$1/ [[b]R=301[/b],L]

Also, be aware that a negative-match pattern may be more efficient than the three-range compare "^[a-zA-Z0-9]+$" pattern. For example consider "^[^/.]+$", which means "one or more characters not a slash or a period." Or at the least, you could use "^[a-z0-9]+$" and the [NC] flag to reduce that to only two range compares:

RewriteRule ^([^/.]+)$ http://www.example.com/$1/ [R=301,L]
-or-

RewriteRule ^([^a-z0-9]+)$ http://www.example.com/$1/ [[b]NC[/b],R=301,L]

Jim

stevej444

6:15 am on May 22, 2007 (gmt 0)

10+ Year Member



Great, thanks again Jim .... very helpful