开发者

Not sure how to get mod_rewrite to write to a certain format

开发者 https://www.devze.com 2023-02-20 15:55 出处:网络
I\'m trying to understand how to properly use mod_rewrite. I have posts in my database that have a title and date. Right now I\'m using mysite.com/post.php?id=3 to retrieve my post data and populate t

I'm trying to understand how to properly use mod_rewrite. I have posts in my database that have a title and date. Right now I'm using mysite.com/post.php?id=3 to retrieve my post data and populate the page. I would like to have it like this: mysite.com/2011/03/27/my-title-like-so/. I'm guessing I'll have to query my da开发者_运维技巧tabase for something other than the id, but i dont know. Can anyone help? Perhaps I'm over looking something very simple. So far I have it showing up like this: mysite.com/post/2/ , but that doesn't help very much :P

Thanks!


A common practice is to include both the ID and the title/date in the URL. That way it looks nice to search engines and you can still retrieve it efficiently by ID. As an example, look at the URL for this stackoverflow question.

http://stackoverflow.com/questions/5451267/not-sure-how-to-get-mod-rewrite-to-write-to-a-certain-format

Just a guess but that number in the URL is probably a generated ID. So you use the ID and just ignore the rest of the URL, e.g.:

RewriteRule /questions/(\d+) question.php?id=$1


If you do not wish to include the ID in the URL, then I would suggest the following approach.

In your "posts" table, include a "url" (or "friendly" or something) field that contains the url you wish your post to be accessed by. When you insert the post, simply strip all non-word characters and replace them with a '-'.

Then, when the URL comes into your system, you can just map this url directly to the "url" field.

So, using your example, your URL would be http://mysite.com/2011/03/27/my-title-like-so/ The "url" field would contain 'my-title-like-so'.

You could use a simple rewrite rule such as the following:

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f # if requesting file that doesn't exist, use the rule below
RewriteCond %{REQUEST_FILENAME} !-d # if requesting a directory that doesn't exist, use the rule below
RewriteRule ^(.*)$ index.php?myvar=$1 [L,QSA]

This would put the contents of the URL after the domain:

http://mysite.com/[catches everything here]

And would store it in the $_GET global as 'myvar'

Then you can do any processing on that url in php:

$url_string = my_sanitising_code($_GET['myvar']);
$url_array = explode('/', $url_string);

Now you have an array of all the parts of the URL and you can do whatever you wish with them.


You must capture the values that are passed to the engine to extract the data you want. So:

RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ fetch.php?year=$1&month=$2&day=$3&title=$4 [L]

should match your format. Then the http://mysite.com/fetch.php can use the $_GETs values to fetch the database and display the good things.

0

精彩评论

暂无评论...
验证码 换一张
取 消