[英]How to use .htaccess to rewrite all pages to a simple URL?
I have the following situation. 我有以下情况。 I want to point all URLs like this:
我想指出所有这样的网址:
http://localhost/mypage
To a long URL, just like this: 对于长网址,如下所示:
http://localhost/index.php?&page=mypage
And then in PHP do something like this: 然后在PHP中执行以下操作:
include 'pages/' . $_GET['page'] . '.php';
I have tried the following but I get some errors. 我尝试了以下操作,但出现一些错误。
RewriteRule ^(.+)$ index.php?&page=$1 [NC]
Is there any way to do this without listing all pages in .htaccess file? 有没有办法在不列出.htaccess文件中的所有页面的情况下执行此操作?
Try this: 尝试这个:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L]
So if you request http://example.com/about , it will be re-written as http://example.com/index.php?page=about . 因此,如果您请求http://example.com/about ,它将被重写为http://example.com/index.php?page=about 。
I'd also suggest doing some validation on $_GET['page']
in your index.php script too, as people will be able to navigation your file system otherwise. 我还建议您也对index.php脚本中的
$_GET['page']
进行一些验证,否则人们将可以导航您的文件系统。
# 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 will be the same as $_GET['project'] = 1
/ project / 1将与
$_GET['project'] = 1
Your First Line Should Be: 您的第一行应该是:
RewriteEngine On
Then Use 然后使用
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
Instead of 代替
RewriteRule ^(.+)$ index.php?&page=$1 [NC]
This is what you need 这就是你所需要的
RewriteEngine On
RewriteRule ^([a-z]+)$ index.php?&page=$1 [QSA]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.