Php 如何使用.htaccess将所有页面重写为一个简单的URL?
我有以下情况。我希望指向所有URL,如下所示:Php 如何使用.htaccess将所有页面重写为一个简单的URL?,php,apache,.htaccess,Php,Apache,.htaccess,我有以下情况。我希望指向所有URL,如下所示: http://localhost/mypage http://localhost/index.php?&page=mypage include 'pages/' . $_GET['page'] . '.php'; 指向一个长URL,如下所示: http://localhost/mypage http://localhost/index.php?&page=mypage include 'pages/' . $_GET['p
http://localhost/mypage
http://localhost/index.php?&page=mypage
include 'pages/' . $_GET['page'] . '.php';
指向一个长URL,如下所示:
http://localhost/mypage
http://localhost/index.php?&page=mypage
include 'pages/' . $_GET['page'] . '.php';
然后在PHP中执行如下操作:
http://localhost/mypage
http://localhost/index.php?&page=mypage
include 'pages/' . $_GET['page'] . '.php';
我尝试了以下方法,但出现了一些错误
RewriteRule ^(.+)$ index.php?&page=$1 [NC]
有没有办法在不列出.htaccess文件中的所有页面的情况下执行此操作
# This line starts the mod_rewrite module
RewriteEngine on
# If the request is for a file that already exists on the server
RewriteCond %{REQUEST_FILENAME} !-f
# Pass all trafic through the index file (if the requested file doesn't exist)
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
/project/1将与
$\u GET['project']=1相同
您的第一行应该是:
RewriteEngine On
然后使用
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
而不是
RewriteRule ^(.+)$ index.php?&page=$1 [NC]
这就是你需要的
RewriteEngine On
RewriteRule ^([a-z]+)$ index.php?&page=$1 [QSA]
试试这个:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L]
因此,如果您要求http://example.com/about,它将被重新写入http://example.com/index.php?page=about
我还建议对index.php脚本中的
$\u GET['page']
进行一些验证,否则人们将能够导航您的文件系统。这很奇怪。它试图加载页面/index.php.php
。谢谢,但现在我有另一个问题。我的网站现在无法正确加载Javascript和CSS。有点不对劲。