问题描述:

I have just finished writing a new site. It is a simple blog. The only advice I have received from my readers is that I should consider changing the www.example.com/?page=3 to something like www.example.com/1.

How should I go about writing this rewrite rule?

网友答案:

If your URLs have a common pattern and parts of the externally used URL can directly be mapped onto the internally used URL while retaining a uniquely identifiable URL (like your URL probably does), you can do something like this with mod_rewrite:

RewriteEngine on
RewriteRule ^[0-9]+$ /?page=$1 [L,QSA]

This will rewrite a request of /12345 internally to /?page=12345.

Otherwise, if there isn’t a pattern or the mapping is not trivial, you will probably need to specify each case like:

RewriteEngin on
RewriteRule ^foo$ /?page=1 [L,QSA]
RewriteRule ^bar$ /?page=2 [L,QSA]
RewriteRule ^baz$ /?page=3 [L,QSA]

You could also just pass the request to your PHP file and do the mapping in there.

相关阅读:
Top