Forum Moderators: phranque

Message Too Old, No Replies

Mod_Rewrite issue

         

rodger99

12:30 pm on Feb 14, 2005 (gmt 0)

10+ Year Member



Hi,

I am looking to create a simple mod_rewrite, although don't know where to start. I needs to add a / onto the end of folder URL's in the address bar.

e.g.

www.sweaters.com/knitted

would rewrite to

www.sweaters.com/knitted/

It cant just add / onto the end of everything though, just subfolders.

e.g.
www.sweaters.com/knitted/new
or
www.sweaters.com/knitted/test.html
should not have a / added to the end of them

I also need it to be as generic as possible, preferably not mentioning www.sweaters.com or /knitted/ so that it can be re-used on other websites or folders.

Thanks for any help you can offer me. If you only know how to do part of it, please post your ideas so that we can solve it together.

Regards,

Rodger

topr8

1:01 pm on Feb 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



you could easily write something generic if you wanted to change all

www.example.com/example

type addresses to

www.example.com/example/

(you could write an expression to rewrite those and not anything that ended in .htm, .html, .php etc)

but if you need to differentiate between

www.example.com/knitted

www.example.com/new

then it would not be possible

if you structure is only one folder deep, then it would be possible to rewrite all url's of the type

example.com/firstlevel

to
example.com/firstlevel/

this is very easy using a generic regular expression

rodger99

1:13 pm on Feb 14, 2005 (gmt 0)

10+ Year Member



Hi,

Thank you for your reply.

How would we go about making a generic ReWrite to add a / onto the end of anything? I'm guessing it would need to filter out anything with a . in it for files.

Thanks :)

Regards,

Rodger

rodger99

3:34 pm on Feb 14, 2005 (gmt 0)

10+ Year Member



Hi,

Can you see any problems in this code, I'm getting an Internal Server Error? Should this do what I want it to do?


ErrorLog error_log
Options +FollowSymLinks
RewriteEngine On
RewriteRule ([^/]*)$ /knitted/$1 [L]

topr8

4:29 pm on Feb 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



try

RewriteRule ^([a-zA-Z0-9]+)$ $1/

the expression needs to start with ^
this expression will add a / to any url in the format

www.example.com/onefolderdeep

as long as the foldename consists of alphanumeric characters only

jdMorgan

5:26 pm on Feb 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



rodger99,

Welcome to WebmasterWorld!

If you are getting a 500-Server Error, then check your server error log! It will often tell you exactly what is wrong.

Externally redirect any request to first-level subdirectories (only) with no trailing slash and not containing a "." to same URL with trailing slash:


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

The above requires a first-level directory path, followed by a slash, followed by a single path element that does not contain any slashes or periods.

-----

I initially misunderstood your requirements and wrote the following. Maybe it will help someone else...

Externally redirect any request one or more subdirectories deep with no trailing slash and not containing a "." to same URL with trailing slash:


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

The above requires *some* directory path, followed by a slash, followed by a single path element that does not contain any slashes or periods.

-----

Both examples above are intended for use in .htaccess. For use in httpd.conf, add a leading slash to the RewriteRule patterns.

Jim