.htaccess : date range help
Hi my 开发者_Python百科URL is something like http://www.mysite.come/events.php?eid=10
Event starting date is 01-07-2011
Event name : summer trip
I want to display the above URL to be like http://www.mysite.com/events/01/07/2011/summer-trip
Does this make sens!?
I will appreciate all your answers, please help as soon as possible.
Thanks
Try something like:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
It's a catch all redirect - if a file doesn't exist to match a request, the request is passed to index.php with the original url. (it's how most of the frameworks, wordpress etc do it)
Then you will have $_GET['url'] available in index.php, which you can parse in php and do whatever needs doing to check for/load relevant content.
Note that you need to ensure you implement a 404 error in PHP here - as apache will never 404 itself any more.
If you just want to send the date in a friendly format, it's quite a simple redirection:
# Untested example
RewriteEngine On
RewriteRule ^events/(\d{2})/(\d{2})/(\d{4})/.* events.php?date=$3-$2-$1
However, mod_rewrite won't do magic. There's no way to obtain 10
from 01/01/2011/summer-trip
.
Edit: I guess I should not take the question literally and provide some hints. If you want to link events by a friendly string, you need to do a couple of internal changes:
Add a new column to the events table in your database and make it unique (most DBMS allow to create unique indexes). That new column (let's call it
url_title
) will hold the friendly string.Adjust your PHP script so it takes the
url_title
as argument, rather than the numeric key.
And your mod_rewrite rule will be something like this:
# Again, untested
RewriteEngine On
RewriteRule ^events/\d{2}/\d{2}/\d{4}/([^/]+)/?$ events.php?url_title=$1 [L]
精彩评论