Welcome to WebmasterWorld Guest from 54.162.226.212

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

modrewrite - 404 when no / at end of url

modrewrite - why do I get a 404 error when no slash is appended to the url

     
11:00 pm on Jan 8, 2006 (gmt 0)

10+ Year Member



Hi all,

I only learnt the benefits of .htaccess after I'd designed my site structure so my .htaccess isn't as efficient as I'd like it to be.

Anyway, when viewing my webstats, I seen a load of 404 errors and on further investigation I found that the reason is the way I rewrite urls.

If a url is requested without a "/" appended to the end of the url, I get the 404 page not found error.

E.g www.mydomain.com/list_a

Heres my .htaccess

--------
RewriteEngine on
RewriteRule ^([^_]+)_([^_]+)/ /index.php?$1=$2 [L]
RewriteRule ^index\.html$ /index.php [L]
----------

If I add another line (I.e RewriteRule ^([^_]+)_([^_]+) /index.php?$1=$2 [L]) for a url without the slash, all my images stop working?!

Can anyone help me with this? Sorry if I've not explained very well!

Many thanks

1:07 am on Jan 9, 2006 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



In order to help, we need to know what the local URL-paths look like -- the ones that match this rule:

RewriteRule ^([^_]+)_([^_]+)/ /index.php?$1=$2 [L]

It's important to know what URL-paths should match that rule, and equally important to know what URLs should not match that rule.

Jim

[edit] Speling [/edit]

[edited by: jdMorgan at 1:32 pm (utc) on Jan. 9, 2006]

7:23 am on Jan 9, 2006 (gmt 0)

10+ Year Member



I've always used the slash at the end of the url but for some unknown reason, Yahoo don't use the slash when they direct users to my site.

(Is this what you needed to know or is it the actual url structure?)

5:43 pm on Jan 11, 2006 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



No, I'm looking for examples of the actual URLs that you intend to rewrite with this rule:

RewriteRule ^([^_]+)_([^_]+)/ /index.php?$1=$2 [L]

The simple answer is to make the trailing slash optional:

RewriteRule ^([^_]+)_([^_]+[b])/?$[/b] /index.php?$1=$2 [L]

but this can have unforeseen side-effects on URLs that are in a form other than exactly

"/<something>_<something>/" or "/<something>_<something>"

Jim

11:09 pm on Jan 12, 2006 (gmt 0)

10+ Year Member



It's a difficult one to explain as I'm so new to rewrites.

---> Level One

[mydomain.com...]

---> Level Two
[mydomain.com...]
[mydomain.com...]
[mydomain.com...]

---> Level Three
[mydomain.com...]
[mydomain.com...]

I have the above working fine using...

----------------------------------------

RewriteEngine on

RewriteRule ^([^_]+)_([^_]+)/([^_]+)_([^_]+)/([^_]+)_([^_]+)/ /index.php?$1=$2&$3=$4&$5=$6 [L]
RewriteRule ^([^_]+)_([^_]+)/([^_]+)_([^_]+)/ /index.php?$1=$2&$3=$4 [L]
RewriteRule ^([^_]+)_([^_]+)/ /index.php?$1=$2 [L]
RewriteRule ^index\.html$ /index.php [L]

----------------------------------------

But as soon as the slash isn't used, everything falls apart. The level three urls work without the slash but the last variable isn't picked up (I've found a way around this though).

The main problem is level 2 as without the slash, the visitor gets a 404. The?$ didn't work for me :(

Edit - Original thread is here [webmasterworld.com]

Edit - I've sent you a pm with my site url for reference if you should need it (not asking for personal support I'd just prefer not to post it here).

1:56 am on Jan 13, 2006 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



> The level three urls work without the slash but the last variable isn't picked up.

That's because they then match the second rule - the unanchored two-name/value-pair rule, which drops the third name/value pair (no $5=$6 in the second rule...

So what happens if you stick a question mark on the trailing slash of each pattern to make it optional, end-anchor them, and fix the patterns? (I suggest you look up what [^x]+ means in the regex tutorial cited in our forum charter before continuing, it will help to understand what was wrong with your patterns - highlighted in 1st rule only.)


RewriteRule ^([^_]+)_([b][^/][/b]+)/([^_]+)_([b][^/][/b]+)/([^_]+)_([b][^/][/b]+)/?$ /index.php?$1=$2&$3=$4&$5=$6 [L]
RewriteRule ^([^_]+)_([^/]+)/([^_]+)_([^/]+)/?$ /index.php?$1=$2&$3=$4 [L]
RewriteRule ^([^_]+)_([^/]+)/?$ /index.php?$1=$2 [L]
RewriteRule ^index\.html$ /index.php [L]

If that does not work, you've got other rules interfering, a problem with mod_dir, stray undescores or slashes in a few URLs, or a combination of these...

Jim

5:17 am on Jan 13, 2006 (gmt 0)

5+ Year Member



If a url is requested without a "/" appended to the end of the url, I get the 404 page not found error.

I think this should solve your problem of trailing slash.

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R]

 

Featured Threads

Hot Threads This Week

Hot Threads This Month