htaccess 如何用 get 做空 url

htaccess how to short url with get

我想用这个做空 url:

https://example.com/?page=home

https://example.com/home

但是我得到这个错误:

Not Found
The requested URL was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

我的 index.php 在文件夹中:/

我的页面 home.php 等在文件夹中:/pages

我的代码

RewriteEngine On
RewriteBase /

# Prevent looping
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

# Rewrite if not index.php page
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ index.php?page= [QSA,L]

# Change url from page={something} to {something} and redirect
RewriteCond %{REQUEST_URI} /index.php
RewriteCond %{QUERY_STRING} page=(.*)
RewriteRule . %1? [L,R]

文件结构

/
-index.php (File)
-.htaccess (File)
-Images (Folder)
-Css (Folder)
-Pages (Folder)
 --Home.php

第一个解决方案: 根据您显示的示例,请您尝试以下操作。这认为您在浏览器中点击 url https://example.com/home

RewriteEngine ON
Options -MultiViews
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?page= [L]


第二个解决方案: 或者如果您在浏览器中点击 https://example.com/?page=home 然后尝试以下操作。

RewriteEngine ON
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^/?$ https://%{HTTP_HOST}/%{QUERY_STRING} [R=301]
RewriteRule ^(.*)/?$ index.php?page= [L]

注意: 请确保您一次只放置其中的一个规则集,因为它们是为不同的目的而编写的.