Forum Moderators: phranque
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
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]
RewriteRule ^([^_]+)_([^_]+)/ /index.php?$1=$2 [L]
RewriteRule ^([^_]+)_([^_]+[b])/?$[/b] /index.php?$1=$2 [L]
"/<something>_<something>/" or "/<something>_<something>"
Jim
---> 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).
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]
Jim