【问题标题】:Url rewriting rule with .htaccess file使用 .htaccess 文件的 URL 重写规则
【发布时间】:2020-02-07 14:23:28
【问题描述】:

我来这里是想就我无法按照自己的意愿配置的 .htaccess 文件征求意见。

我有一个网站,比如说: www.mysite.com/php/blog.php

我希望访问者只在地址栏中看到: www.mysite.com/blog

这是 .htaccess 文件包含的内容:

Options +FollowSymlinks
Options All -Indexes
RewriteEngine On
RewriteRule ^$ index.php     #This line is to hide index.php on the home page, maybe it's not the right way to do it
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^php/([0-9a-zA-Z]+).php$ $1 [L]            #I tried to write this, but it doesn't work
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

我怀疑这个问题已经问过很多次了,但是我真的试过了,没有成功。

你能帮帮我吗? 谢谢!

【问题讨论】:

    标签: .htaccess url-rewriting


    【解决方案1】:

    您的模式错误地期望 URI 以 /php/ 开头,这不是这里的情况。还要将http -> https 规则保留在顶部,然后像这样处理.php 扩展规则:

    RewriteEngine On
    
    # http -> https redirect
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
    
    # To externally redirect /php/file.php to /file
    RewriteCond %{THE_REQUEST} \s/+(?:php/)?(\S+?)\.php[\s?] [NC]
    RewriteRule ^ /%1 [R=301,L,NE]
    
    # To internally forward file to /php/file.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/php/$1.php -f
    RewriteRule ^(.+?)/?$ php/$1.php [L]
    

    【讨论】:

    • 您好,anubhava,感谢您的宝贵时间!不,不幸的是它不起作用。顺便说一句,我希望在地址栏中隐藏文件夹的路径 /php/ 以及 .php 扩展名。我想获得“www.mysite.com/contact”而不是“www.mysite.com/php/contact.php”
    猜你喜欢
    • 2017-07-16
    • 2016-03-30
    • 2012-08-20
    • 2011-12-06
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    相关资源
    最近更新 更多