如何将旧的URL重定向到URL,但同时拥有漂亮的URL时,两个URL仍然可以使用

时间:2019-11-20 07:15:35

标签: php .htaccess url-rewriting

这是我的旧网址

http://localhost/site/viewactors.php?name=G%20Eazy%20ft%20Blac%20Youngsta%20and%20BlocBoy%20JB%20Drop

但是新的网址看起来像这样:

http://localhost/site/viewactors/G-Eazy-ft-Blac-Youngsta-and-BlocBoy-JB-Drop

如何使用.htaccess重定向,将新网址重定向到与查询正确的同一viewactors页面,但是如果有人访问了旧网址,它仍然仍然可以工作

这是我目前拥有的

RewriteCond %{viewactors}.php -f
RewriteRule !.*\.php$ %{viewactors}.php [QSA,L]
RewriteRule ^([-\w]+)$ /viewactors.php?p=$1 [NC,L]

5 个答案:

答案 0 :(得分:1)

尝试以下规则,

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^viewactors/(.*)$ viewactors.php?name=$1 [NC,L]

答案 1 :(得分:1)

像这样修改您的.htaccess代码

RewriteEngine on
RewriteRule ^([a-zA-Z0-9-/]+)$ viewactors.php?name=$1

从下面的网址

http://localhost/site/viewactors/G-Eazy-ft-Blac-Youngsta-and-BlocBoy-JB-Drop

现在您可以像这样获得G-Eazy-ft-Blac-Youngsta-and-BlocBoy-JB-Drop的值(在viewactors.php文件中)

echo $_GET['name'];

答案 2 :(得分:0)

尝试一下,希望它能解决您的查询:)

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^/]+)/?$ viewactors.php?p=$1 [L]

答案 3 :(得分:0)

尝试以下代码:

RewriteEngine On
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]

RewriteRule ^(.*)/ /$1.php [QSA,L]
RewriteRule    ^viewactors/([A-Za-z0-9-]+)/?$    /viewactors.php?name=$1 [QSA,L]

在viewactors.php页面中,您可以将以下代码放在页面顶部,以检索名称:

$param = explode("/",str_replace("'", "''", $_SERVER['REQUEST_URI']));
if(isset($param[count($param) - 1]) && $param[count($param) - 1] != ""){
    $name = $param[count($param) - 1];
    echo "Viewactor name = " . $name;
}

答案 4 :(得分:0)

好的,我认为这对我有用: 在RewriteEngine上 RewriteRule ^ viewactors.php /([[-a-zA-Z0-9 _] +)/? viewactors.php?&name = $ 1 [L]

我最终得到的URL是这样的:http://localhost/site/viewactors/G-Eazy-ft-Blac-Youngsta-and-BlocBoy-JB-Drop

旧网址仍然有效