简体   繁体   中英

How to rewrite urls like this?

How would I rewrite ( for every posts in my blog ):

http://localhost/post.php?id=13

to

http://localhost/this-is-the-title-of-the-post/13

AND ( without id )

http://localhost/post.php?id=13

to

http://localhost/this-is-the-title-of-the-post/

Try this rule:

RewriteEngine on
RewriteRule ^[^/]+/(\d+)$ post.php?id=$1

This will rewrite a requested URI path like /this-is-the-title-of-the-post/13 internally to /post.php?id=13 ; so it's rather the other way round of what you described. But that's how mod_rewrite works: You can only change incoming requests and not outgoing responses.

Now if you want to omit the numeric ID, your title needs to be the new ID:

RewriteRule ^this-is-the-title-of-the-post/(13)?$ post.php?id=13

That means it needs to be unique. Otherwise the title cannot be mapped onto the numeric ID unambiguously. In that case the title it would certainly be easier if you pass the title to post.php :

RewriteCond %{REQUEST_FILENAEM} !-f
RewriteRule ^([^/]+)/$ post.php?title=$1

Obviously, since the title is unique, you don't need the numeric ID any more.

mod_rewrite is an Apache module which allows you to do this using regular expressions. If you're using a Windows/IIS web server, you'll have entirely different techniques; I'll just stick with Apache.

Your first example is quite easy because the rewritten URL just needs to contain the ID in the correct place for it to work. The previous answer has already given a workable solution.

The second example (ie without the ID in the rewritten URL) would need to you modify your underlying PHP to query the DB to lookup the page title rather than the ID (This may have performance implications for your site, depending on how you've written it). Alternatively, you could have a specific rewrite rule for each individual URL, but that is quite an overhead to maintain.

The bad news with mod_rewrite is that it can be quite a dark art to get it working, expecially if you're not good with regular expressions. The trouble is that if it doesn't work you just get a server config error in your browser, and it can be very hard to debug. It is worth the effort if you're willing to persevere, but it can be frustrating for a beginner.

The good news is that if you're using a CMS product such as Wordpress or Drupal, these have URL rewrite functionality built in, so you don't need to mess around with server config at all.

Hope that helps.

If you want to have just text only URLs (without pk reference in URL), you must implement unique SLUG field in database (which can be unique for date, like wordpress does).
I suggest redirecting all requests, which does not match existing files to index.php and deal with $_SERVER['REQUEST_URI'] and avoid handling mod_rewrite magick (with in long term leads to unmaintanable and magick code). mod_rewrite rule for this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM