问题
My site currently displays like this (when clicking on an ad):
hxxp://mysite.com/viewItem.php?id=10
I would LOVE for the urls to read:
hxxp://mysite.com/dogs/121/border-collie-for-sale-to-good-home
where "dogs" is forever a constant, 121 is always the $row['postId']; and "border-collie-for-sale-to-good-home" is always my $row['title'];
I know it probably won't be so easy as a quick answer here but would appreciate it if you could get me going in the right direction. I'm assuming a change to .htaccess is in order and I am just terrible at trying to decipher that.
回答1:
Something that you can do is add a redirect at the top of the viewItem.php script. This redirect will need to check for a query parameter that you use to indicate that the .htaccess file as rewritten. Something like this:
if( !isset($HTTP_GET_VARS['rewritten']) ) {
// use whatever behind the scenes stuff you need to construct the friendly URL
$friendly_url = "http://mysite/" . getCategory() . "/" . getID() . "/" . getTitle();
// Now redirect the browser:
header("HTTP/1.1 301 Moved Permanently");
header("Location: $friendly_url");
exit();
}
// business as usual, the rest of your viewItem.php script.
So when this php script tries to handle a request that DOESN'T have the rewritten
parameter in the query string, it redirects to the friendly version of the URL. If it DOES have rewritten
it does what it usually does and handles the request.
Now in the .htaccess file, you want to rewrite the ugly URL to the friendly URL, but you need to include rewritten
in the rewrite (I assume the "121" in your example is the "id" that the viewItem.php script takes:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]+/([^/]+)/ /viewItem.php?id=$1&rewritten=true [L]
If you leave out the &rewritten=true
there, it's going to go into a redirect loop. You can also add a QSA
in the brackets of the rule if you want to pass other query strings along with the rewrite in case you want to be able to handle friendly URLs like: http://mysite/dogs/121/border-collie-for-sale/?sort=asc With [L,QSA]
, the "sort=asc" gets passed along to viewItem/php.
So this is what happens now when someone clicks on an ad and gets taken to http://mysite/viewItem.php?id=121:
- The browser location bar says
http://mysite/viewItem.php?id=121
request is sent to mysite and viewItem.php is accessed - The top of viewItem.php sees that there is no
rewritten
param, so it redirects the browser to http://mysite/dogs/121/border-collie-for-sale - Since that was a 301 redirect, the browser's location bar changes to http://mysite/dogs/121/border-collie-for-sale
- Request is resent to mysite but this time, the .htaccess rewrites
/dogs/121/border-collie-for-sale
to/viewItem.php?id=121&rewritten=true
INTERNALLY, so the browser's location bar does not change. - The request makes it back to viewItem.php, this time, the script sees that there is a
rewritten
parameter, and does it's usual business and the request is served
You can use whatever name for the parameter flag you want, as long as none of the scripts are using it.
来源:https://stackoverflow.com/questions/7974421/how-can-i-make-my-urls-friendly-from-this-example