homepage Welcome to WebmasterWorld Guest from 54.224.179.98
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
modrewrite - 404 when no / at end of url
modrewrite - why do I get a 404 error when no slash is appended to the url
PumpkinHead




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

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

 

jdMorgan




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

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]

PumpkinHead




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

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?)

jdMorgan




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

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

PumpkinHead




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

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).

jdMorgan




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

> 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

milanmk




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

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]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved