Htaccess使“;漂亮的“;URL


Htaccess to make "pretty" URLs

我试图制作漂亮的URL,但它似乎什么都没做。这是我的第一次尝试,我正试图根据指南来做这件事,所以我觉得我错过了什么,或者事情没那么简单。也许有人能帮忙?有什么建议吗?我试着用生成器来做这件事,但没有一个生成器生成能工作的代码。

供您参考,这是当前的URL格式:domain.com/articles.php?page=1

我正试图把它变成这样:domain.com/articles/page/1/

这是我在里面写的。htaccess

RewriteEngine on
RewriteBase /
RewriteRule    ^articles/([0-9]+)/?$    articles.php?page=$1    [NC,L]

提前感谢您的帮助。

对于这个漂亮的URL:domain.com/articles/page/1/:

您的正则表达式似乎是错误的。试试这个:

RewriteEngine on
RewriteBase /
RewriteRule ^articles/[^/]+/([0-9]+)/?$  articles.php?page=$1 [NC,L]

前段时间我写了下面的一个,优点是:

  • 您将控制器和操作存储在zend中
  • 您可以拥有任意多的参数
  • 您可以将css、js和图像存储在文件夹/public中/

httacess 中的代码

RewriteEngine On
RewriteBase /
RewriteRule ^/?public/.+$ - [L]
RewriteRule ^([a-z]+)/([a-z]+)/(.*)$ index.php?controller=$1&action=$2&params=$3 [L]
RewriteCond %{QUERY_STRING} ^(.*)
RewriteRule ^([a-z]+)/([a-z]+)?(.*)$ index.php?controller=$1&action=$2&%1 [L]
RewriteRule ^([a-z]+)/([a-z]+)$ index.php?controller=$1&action=$2 [L]
RewriteRule ^([a-z]+)$ index.php?controller=$1 [L]
RewriteRule ^ index.php [L]

相反,在引导程序(index.html)中,您必须在开头添加以下内容:

/*
 * Split params from given URL
 */
$_PARAMS = array();
foreach ($_GET as $key => $value) {
        $_PARAMS[$key] = $value;
}
if (isset($_GET['params'])) {
        $_TEMP = explode( ",", $_GET['params'] );
        for ($i = 0; $i+1 < count($_TEMP) ; $i+=2) {
                $_PARAMS[$_TEMP[$i]] = $_TEMP[$i+1];
        }
}
$controller = "";
$action = "";
/*
 * load controller -> layout -> action
 */
$controller = isset($_GET['controller']) ? $_GET['controller'] : "";
$action = isset($_GET['action']) ? $_GET['action'] : "index";

并且您可以从$_params 中获得参数