如何使用preg_replace和htaccess修改url?

时间:2015-11-09 18:58:00

标签: php .htaccess mod-rewrite preg-replace

我根据你的建议修改了它

<a class="upheader" href="leagues/' . $rs['ext'] . '/' . $rs['uniqueid'] . '" target="_blank">' . $rs['title'] . '</a>

现在,我将/ leagues / pl / xxxxxx作为新网址

然而,它找不到页面,显示找不到文件(404错误)

这是我的整个.htaccess文件

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule leagues/pl/([0-9]+)/?$ pl?uniqueid=$1 [L]

在我之前的例子中,我使用'pl'而不是$ rs ['ext']来更容易解释。另外,我不知道如何在.htaccess中使用变量

如果您能告诉我如何使用preg_replace或任何其他相关功能以便我的代码看起来和像

一样工作,我将非常感激。
<a class="upheader" href="leagues/' . $rs['ext'] . '/' . preg_replace('/\s\s+/', $rs['uniqueid']) . '" target="_blank">' . $rs['title'] . '</a>

并且网址显示类似

/联赛/ PL / XXXXXX

pl是变量

在.htaccess

的帮助下

1 个答案:

答案 0 :(得分:0)

以我的.htaccess为例:

# Rewrites
RewriteEngine           On

# every html-element that uses a src="" needs these:
RewriteCond             %{REQUEST_URI}          ^/styles/.*$
RewriteRule             ^styles\/(.*)           /sites/%{SERVER_NAME}/styles/$1         [L]

RewriteCond             %{REQUEST_URI}          ^/includes/.*$
RewriteRule             ^includes\/(.*)         /sites/%{SERVER_NAME}/includes/$1       [L]

RewriteCond             %{REQUEST_URI}          ^/images/.*$
RewriteRule             ^images\/(.*)           /sites/%{SERVER_NAME}/images/$1         [L]

# everything else will be directed to the requesthandler:
RewriteCond             %{REQUEST_URI}          !^/core/libraries/FCKeditor/.*$
RewriteCond             %{REQUEST_URI}          !/cache/.*$
RewriteCond             %{REQUEST_URI}          !/images/.*$
RewriteCond             %{REQUEST_URI}          !/styles/.*$
RewriteCond             %{REQUEST_URI}          !/includes/.*$
RewriteCond             %{REQUEST_URI}          !^/requesthandler.php
RewriteRule             (.*)                    /requesthandler.php                     [L]

requesthandler.php使用$ _SERVER [&#39; REQUEST_URI&#39;]从数据库或文件系统中检索请求的数据。