|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
mod_rewrite - Rewrite to a 'default' if rules not met
I'm usually pretty good with Apache config and mod_rewrite in general, but I can't figure out why I'm getting 404's returned here.
Setup: PHP MVC test application - 'Blog' .htaccess Code:
RewriteEngine On
RewriteBase /blog
RewriteRule ^(\/)*$ Blog.php
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/?$ Blog.php?blog_date=$1-$2-$3
RewriteRule ^([0-9]+)/?$ Blog.php?blog_id=$1 [L]
As you can see, the first rule maps what would normally be an index page to 'Blog.php'. The two subsequent rules of course map a year/month/day and id value to the PHP script respectively. The last rule gets the L flag. The default rules work as-is. All these URL's work: http://thedomain/blog http://thedomain/blog/ http://thedomain/blog/2008/04/18 http://thedomain/blog/2008/04/18/ http://thedomain/blog/1234 http://thedomain/blog/1234/ It's after the trailing slashes where I run into problems. For example, if I enter something arbitrary after the slash, e.g. http://thedomain/blog/test http://thedomain/blog/2008/04/18/test http://thedomain/blog/1234/test I get nothing but 404's returned. What I need to happen is either the trailing strings are ignored or a 'default rule' will map any arbitrary (read: wrong) URL back to the base URL. TIA
__________________
BookMooch.com : Give books away. Get books you want. |
|
#2
|
||||
|
||||
|
Two quick solutions I see would be to use DirectoryIndex to set your default page to Blog.php and ErrorDocument 404 to handle arbitrary URLs. I'm guessing you have reasons to not take this approach, so on to your problem ...
^(\/)*$ matches a forward slash zero or more times, so it will not match an arbitrary URL. You would want something like ^/?.*$ to do that, but you will need to move this to the last RewriteRule to prevent matching your valid URLs. You should also add the [L] flag to all RewriteRules unless you know why you don't need to. Actually, I believe you'll have to in order to prevent your catch all from catching an already rewritten URL.
__________________
# Jeremy Explain your problem instead of asking how to do what you decided was the solution. |
|
#3
|
|||
|
|||
|
Thanks for your reply; I'm still in the process of working out the bugs, but I eventually changed the index script to 'index.php' and use the last rewrite rule you suggested. All is working well now.
|
![]() |
| Viewing: Dev Shed Forums > System Administration > Apache Development > mod_rewrite - Rewrite to a 'default' if rules not met |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|