.htaccess 删除“?page=”

问题描述

我有两个相关的问题:

  1. 使用示例网址:https://www.example.com/?page=foo.php

我如何使用 .htaccess 将其写成:https://www.example.com/foo

第二部分

  1. 使用示例网址:https://www.example.com/?page=directory/bar.php

我如何使用 .htaccess 将其写成:https://www.example.com/directory/bar

编辑:

回复显示您当前的.htaccess

# PHP -- BEGIN cPanel-generated handler,do not edit
# Set the “ea-PHP74” package as the default “PHP” programming language.  
 <IfModule mime_module>
   AddHandler application/x-httpd-ea-PHP74___lsPHP .PHP .PHP7 .phtml
 </IfModule>
# PHP -- END cPanel-generated handler,do not edit

## Redirects the index.PHP file to the root domain
RewriteRule ^index.PHP$ https://www.digitleSEO.com/ [R=301,L]

#redirect  /file.PHP to /file
RewriteRule ^(.+).PHP$ /$1 [L,R]
# Now we will internally map /file to /file.PHP
RewriteCond %{REQUEST_FILENAME}.PHP -f
RewriteRule ^(.*)/?$ /$1.PHP [END]

解决方法

这应该对你有用。

RewriteEngine on
#redirect /?page=foobar.php to /foobar
RewriteCond %{THE_REQUEST} \s/(?:index\.php)?\?page=([^.]+)\.php [NC]
RewriteRule .* /%1? [L,R]
###the rule bellow will internally forward the new URL to the old one
# not an existent file
RewriteCond %{REQUEST_FILENAME} !-f
#not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#rewrite the request /foo/bar to /?page=foo/bar.php
RewriteRule ^(.+)/?$ /?page=$1.php [L,END]
,

这是我的文件 htaccess,我用它来删除控制器并将控制器名称保留为路径,将操作名称保留为路径。

<IfModule mod_rewrite.c>

    RewriteEngine on
    ErrorDocument 404 http://shop.local/error/index

    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f

    RewriteRule ^(.*)/(.*) index.php?controller=$1&action=$2
</IfModule>