首页 > 解决方案 > .htaccess 没有文件名的 URL 重定向

问题描述

我的 PHP 页面重定向有问题。

我担心的是我有一个名为“page.php”的页面,该页面还包含重定向期间的一些参数。

www.exampleurl.com/page/param-1

在这些网址中,参数是这样的

Array
{
   [0] => www.exampleurl.com  //URL
   [1] => page  //page.php page where the page will redirect
   [2] => param-1  //GET parameter for page.php
}

并且重定向在 page.php 上使用参数正确完成

但我需要这样的网址

www.exampleurl.com/param-1

使用这个 URL,我要求它仍然重定向到 page.php 而不是这个 URL 没有“page/”并且只有参数。或者如果有人想要请求名为“page.php”的页面,我不需要“page/”,但页面想要重定向到 page.php

我正在使用的 .htaccess 规则

RewriteEngine On    # Turn on the rewriting engine
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1                   [R=301,L]

# not rewrite css, js and images
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpg|gif|png)$           [NC]
RewriteRule  ^page/(.*)$/?$         page.php?slug=$1            [NC,L]

我需要进行哪些更改?

标签: phpregex.htaccessurl-redirection

解决方案


在 .htaccess 中重写后添加此规则

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /page.php?slug=$1 [L]

重写示例:/param-1 到 /page.php?slug=param-1


推荐阅读