代码之家  ›  专栏  ›  技术社区  ›  LOTUSMS

从URL请求中删除.php文件扩展名

  •  2
  • LOTUSMS  · 技术社区  · 7 年前

    我试图美化我的网址,并希望摆脱.php文件扩展名我在这里发现了一些其他的问题,比如这个( Removing the PHP file type extension )但这些建议对我都不起作用他们将我发送到我的404.php或根本不更改URL。

    我认为htaccess中的整个RewriteEngine代码本身是冲突的。

    这是我到目前为止得到的,

    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
        RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
    
        RewriteCond %{SERVER_PORT} !^443$
        RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    
        RewriteBase /
        RewriteRule ^index\.php$ - [R]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /404.php [R]
    </IfModule>
    

    我希望我的域名看起来像

    https://www.example.com/about

    3 回复  |  直到 7 年前
        1
  •  2
  •   anubhava    7 年前

    不要混在一起 Redirect , RedirectMatch RewriteRule htaccess中的指令来自不同的Apache模块,它们的加载顺序是不可预测的。

    这样做:

    ErrorDocument 404 /404.php
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
    RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301,NE]
    
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    
    # To externally redirect /dir/file.php to /dir/file
    RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
    RewriteRule ^ /%1 [R=301,NE,L]
    
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+?)/?$ $1.php [L]
    

    请确保在测试此更改之前清除浏览器缓存。

        2
  •  1
  •   Fobos    7 年前

    输入.htaccess

    RewriteEngine on
    
    RewriteBase /
    
    RewriteCond %{REQUEST_METHOD} POST
    RewriteRule ^ - [L]
    
    #1)externally redirect "/file.php" to "/file"
    RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
    RewriteRule ^ /%1 [NC,L,R]
    
    #2)Internally map "/file" back to "/file.php"
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*?)/?$ /$1.php [NC,L]
    
    ErrorDocument 404 ./404
    RewriteCond %{REQUEST_URI} ^404/$
    RewriteRule ^(.*)$ ./404 [L]
    
    Redirect 301 /index /
    
        3
  •  0
  •   Mohammad    7 年前

    从链接中删除.php:

       RewriteEngine On
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteRule ^([^\.]+)$ $1.php [NC,L]
    
    推荐文章