我怎样才能让我的网址,从这个例子中友好吗?我的、友好、例子、网址

2023-09-02 10:10:43 作者:人格你有吗

我的网站当前显示像这样(在点击广告时):

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  

,其中狗永远是一个常数,121始终是$行['postId'];和边境牧羊犬出售到好家庭始终是我的$行['标题'];

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'];

我知道它可能不会那么容易,因为在这里一个快速的答案,但将AP preciate它,如果你可以让我在正确的方向。我假设一个变化的.htaccess是为了和我只是可怕在试图破译了。

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.

推荐答案

东西,你可以做的是在viewItem.php脚本的顶部添加重定向​​。此重定向将需要检查您用来指示.htaccess文件的重写查询参数。事情是这样的:

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.

所以,当这PHP脚本试图处理不具有在查询字符串的重写参数的请求,将其重定向到URL的友好的版本。如果它确实有重写它做什么,它通常不会和处理该请求。

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.

现在在.htaccess文件中,您要重写丑陋的URL到友好的URL,但您需要包括重写中重写(我承担的121 在你的例子是ID,该viewItem.php脚本需要:

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]

如果你离开了&放大器;改写=真在那里,它会进入一个重定向循环。您也可以在规则的括号内添加 QSA ,如果你想通过其他的查询字符串以及您希望能够处理友好的URL一样的情况下改写: 的http:// mysite的/狗/ 121 /边境牧羊犬,换销售/?排序= ASC 随着 [L,QSA] ,排序= ASC被沿viewItem / PHP过去了。

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.

所以,这是发生什么事情,当有人点击广告和被采取的http:// mysite的/ ?viewItem.php ID = 121 :

So this is what happens now when someone clicks on an ad and gets taken to http://mysite/viewItem.php?id=121:

在浏览器地址栏说的http://mysite/viewItem.php ID = 121 请求被发送到mysite的和viewItem.php访问 viewItem.php的顶部看到,有没有重写参数,所以它的浏览器重定向到的 HTTP:// mysite的/狗/ 121 /边境牧羊犬出售 在自认为是一个301重定向,浏览器的地址栏更改为的http:// mysite的/狗/ 121 /边境牧羊犬出售 请求重新发送到mysite的,但这个时候,的.htaccess重写 /狗/ 121 /边境牧羊犬出售 / viewItem ?.PHP ID = 121安培;改写= TRUE 在内部,所以浏览器的地址栏并没有改变 请求使它回到viewItem.php,这个时候,剧本看到有一个重写参数,并执行它一贯的企业和请求提供服务 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.