Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用htaccess(apache)重写URL_Php_Apache_.htaccess_Mod Rewrite_Url Rewriting - Fatal编程技术网

Php 使用htaccess(apache)重写URL

Php 使用htaccess(apache)重写URL,php,apache,.htaccess,mod-rewrite,url-rewriting,Php,Apache,.htaccess,Mod Rewrite,Url Rewriting,我有一个网站,在主页上显示多篇文章。每篇文章都有一个链接,当我点击它时,我通过URL将Id、日期和标题作为参数传递到“article.php”页面。当我在文章页面上时,我通过Id识别出哪篇文章,然后显示内容 但我的问题是:当我打开“article.php”页面时,我的URL如下所示 http://127.0.0.1/Andrea/mySite/article.php?id=21&date=02%20march%202017%title=Basket%20NBA:%20Bulls-Warr

我有一个网站,在主页上显示多篇文章。每篇文章都有一个链接,当我点击它时,我通过URL将Id、日期和标题作为参数传递到“article.php”页面。当我在文章页面上时,我通过Id识别出哪篇文章,然后显示内容

但我的问题是:当我打开“article.php”页面时,我的URL如下所示

http://127.0.0.1/Andrea/mySite/article.php?id=21&date=02%20march%202017%title=Basket%20NBA:%20Bulls-Warriors.%20Analysis
我已经创建了.htaccess文件,并且我能够将人们重定向到其他页面,以便启用重写,我搜索的是将URL从上面更改为类似的内容

http://127.0.0.1/Andrea/mySite/article.php?id=21&date=02%20march%202017%title=Basket%20NBA:%20Bulls-Warriors.%20Analysis
http://127.0.0.1/Andrea/mySite/2017/03/02/basket-nba-bulls-warriors

因此,我想从URL中删除point和“article.php”后面的“Analysis”部分,切换日期(如果是文件夹)和标题(用单词之间的分数书写)

我试过了

RewriteEngine on
RewriteRule ^id/([A-Za-z0-9-]+)/?$ article.php?id=$1 [NC]
删除“article.php”并在斜杠之间添加id,但似乎不起作用


谢谢你给所有帮助我的人的建议

带有mod_rewrite的.htaccess路由

在根文件夹中添加一个名为
.htaccess
的文件,并添加如下内容:

RewriteEngine on
RewriteRule ^/?Some-text-goes-here/([0-9]+)$ /picture.php?id=$1
这将告诉Apache为此文件夹启用mod_rewrite,如果询问它与正则表达式匹配的URL,它会在内部将其重写为您想要的内容,而最终用户不会看到它。简单,但不灵活,因此,如果您需要更多动力:

PHP路线

将以下内容放在.htaccess中:

FallbackResource index.php

这将告诉它运行index.php来处理所有它在站点中通常找不到的文件。然后,您可以在其中执行以下操作,例如:

$path = ltrim($_SERVER['REQUEST_URI'], '/');    // Trim leading slash(es)
$elements = explode('/', $path);                // Split path on slashes
if(empty($elements[0])) {                       // No path elements means home
    ShowHomepage();
} else switch(array_shift($elements))             // Pop off first item and switch
{
    case 'Some-text-goes-here':
        ShowPicture($elements); // passes rest of parameters to internal function
        break;
    case 'more':
        ...
    default:
        header('HTTP/1.1 404 Not Found');
        Show404Error();
}

这就是大型网站和CMS系统所做的,因为它允许更灵活地解析URL、配置和数据库相关URL等。对于零星使用,
.htaccess
中的硬编码重写规则将很好。

带有mod_rewrite的.htaccess路由

在根文件夹中添加一个名为
.htaccess
的文件,并添加如下内容:

RewriteEngine on
RewriteRule ^/?Some-text-goes-here/([0-9]+)$ /picture.php?id=$1
这将告诉Apache为此文件夹启用mod_rewrite,如果询问它与正则表达式匹配的URL,它会在内部将其重写为您想要的内容,而最终用户不会看到它。简单,但不灵活,因此,如果您需要更多动力:

PHP路线

将以下内容放在.htaccess中:

FallbackResource index.php

这将告诉它运行index.php来处理所有它在站点中通常找不到的文件。然后,您可以在其中执行以下操作,例如:

$path = ltrim($_SERVER['REQUEST_URI'], '/');    // Trim leading slash(es)
$elements = explode('/', $path);                // Split path on slashes
if(empty($elements[0])) {                       // No path elements means home
    ShowHomepage();
} else switch(array_shift($elements))             // Pop off first item and switch
{
    case 'Some-text-goes-here':
        ShowPicture($elements); // passes rest of parameters to internal function
        break;
    case 'more':
        ...
    default:
        header('HTTP/1.1 404 Not Found');
        Show404Error();
}

这就是大型网站和CMS系统所做的,因为它在解析URL、配置和数据库相关URL等方面提供了更大的灵活性。对于偶尔使用的情况,
。htaccess
中的硬编码重写规则会很好。

谢谢你,但你能说得更清楚吗?或者把它改成我的例子?我很感激。如果你想看例子,请转到这里,谢谢你,但你能说得更清楚一点吗?或者把它改成我的例子?我会很感激的。如果你想看到这个例子,请点击这里