Forum Moderators: phranque

Message Too Old, No Replies

Need Help debugging Mod Rewrite rules

         

pantheon

3:42 am on Mar 14, 2006 (gmt 0)

10+ Year Member



If anyone can help me debug this issue I would be very grateful.

I have two conditions which I want to happen

1) regardless of the url, I want everything to be pointed to a file I specify such as index.php.

the code I have does what I need.

RewriteEngine on
RewriteRule ^(.*)$ index.php [L]

2) I want to put an trailing slash on a folder request in the event users forget to put it in

the code I have for this also works fine for me.

RewriteEngine on
rewritebase /
RewriteRule ^folder_name$ folder_name/ [R=301]

But when I put the rules together neither works.

My goal is to have anyone referencing this folder o check for the trailing slash and input it if it doesn't exist in the url and then for all url values regardless to be pointed to a index.php file. For ex. a url path of article/title/content/content.html would be pointed to a index.php file which does the parsing of the values.

Like I said the rules work separately but not when I put them together.

RewriteEngine on
rewritebase /
RewriteRule ^folder_name$ folder_name/ [R=301]
RewriteRule ^(.*)$ index.php [L]

Can anyone explain to me why they dont work together? and what I need to do to get them to work?

jdMorgan

8:14 pm on Mar 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks to me as if you've created an infinite loop. (I can't tell, because "it doesn't work" doesn't give us much information.)

Add a RewriteCond to prevent the loop:


RewriteEngine on
RewriteBase /
#
# Add missing trailing slash to folder_name and redirect
RewriteRule ^folder_name$ http://www.example.com/folder_name/ [R=301,L]
#
# Rewrite all requests except for index.php to index.php
RewriteCond $1 !index\.php$
RewriteRule (.*) /index.php [L]

A few other syntax fixes as well.

Note that as you specified, all requests are sent to the script, including requests for /folder_name/, robots.txt, custom error pages, all images, CSS stylesheets, etc.

Jim

pantheon

10:50 pm on Mar 14, 2006 (gmt 0)

10+ Year Member



Sorry for not being specific about "what doesnt work"

Like I said they work as intended separately but when I put them together, neither objectives are achieved.

Neither does it put a trailing slash but it also doesnt refer all urls to the index.php file as I intended.

Now I have been playing with the rules a bit and I think mostly the problem is due to the last rule being to broad and its messing up the trailing slash rule as well.

So I have been toying with it to narrow the second rule, and this seems to work. Better anyways. Its more specific about what it is looking for so it does not interfere with the trailing slash routine.

RewriteEngine on

RewriteRule ^([a-zA-Z0-9/]+)/([a-zA-Z0-9]+) /index.php?id=$2 [L]
RewriteRule ^([a-zA-Z0-9]+)/?$ /index.php?id=$1 [L]

I'm like 99% where I want to be.

The only problem here is if there is a non-alpha character in the url like "&" or "-" or "_" it doesnt know what to do.

For ex: if the url looks like [domain.com...]

its giving me a 404 not found error.

With my limited understanding the reason for this is because the conditions I am checking for only take into consideration for alpha or numeric values in the url.

What can I add to the rule to look for non-alpha characters without using the broader ".*" which is whats was causing the problem to begin with?

jdMorgan

11:02 pm on Mar 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> non-alpha character in the url like "&" or "-" or "_"

RewriteRule ^([a-z0-9_&\-]+)/?$ /index.php?id=$1 [b][NC[/b],L]

See the regular-expressions tutorial cited in our forum charter.

Jim

pantheon

11:30 pm on Mar 14, 2006 (gmt 0)

10+ Year Member



Jim,

That fixed it, thanks alot.

Boy this whole thing is new to me but its also pretty fun.

Right now I feel like a sponge just waiting to soak up this stuff.

I'll check out the tutorial like you asked. Also if you have any other websites that you know of that shows practical examples I'd appreciate any pointers.

Thanks again.

pantheon

1:42 am on Mar 15, 2006 (gmt 0)

10+ Year Member



Jim,

Sorry to keep bothering you with this but that last piece brings me to 99.9% now.

The mod rewrite rule now works the way it should most of the time.

However, theres one little anomoly.

when I have a url like [domain.com...]

the value it returns is "first_" not "first_&_second"

if the url is first_second it works fine.

it seems only when the url has the "&" in it, it returns only the first part of the value and not the whole.

I'm using he rewrite rule as you specified in your last post.

Any thoughts?

My suspicion is that "&" is actually used in the url for query in like query=123&id=2

If so is there a work around or maybe I need to parse the values separately?

jdMorgan

12:05 am on Mar 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, "&" is not a valid character in a URL. You can use it only in a query string attached to a URL, but not in the URL itself.

I'd suggest cahnging it to the word 'and' for linking purposes.

Jim