【问题标题】:htaccess rewrite url pages to foldershtaccess 将 url 页面重写到文件夹
【发布时间】:2014-02-28 08:15:24
【问题描述】:

我正在尝试制作一个 .htaccess 页面,该页面可以将我的网址从 www.site.com/about.php 更改为 /About/。目前所有页面都在根目录中,所以我想对多个页面执行此操作。到目前为止,我的 htaccess 文件中有这个:

RewriteEngine On  

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

RewriteRule ^(.*)$ about.php [QSA,L]

这似乎只是将除 index.php 之外的所有页面更改为 about 页面。我在导航中的超链接现在转到 /About/、/Contact/ 等,但我无法弄清楚我需要更改哪些内容才能使不是关于停止显示 about.php 的链接。

任何帮助都会很好,因为我对 PHP 很陌生。提前感谢您的任何指导

【问题讨论】:

    标签: php apache .htaccess url mod-rewrite


    【解决方案1】:

    您正在将所有请求与

    匹配
    ^(.*)$
    

    并将它们发送到“about.php”。

    您要做的是匹配请求,然后使用该匹配来构造文件名:

    # Get the Request
    RewriteCond %{REQUEST_FILENAME} ^(.*)$
    # Check that the file exists, using the match from above (%1)
    RewriteCond %1\.php -f
    
    RewriteRule ^(.*)$ $1.php [QSA,L]
    

    这是一种非常基本的方法。您可能希望增强规则,使其仅匹配第一个 URL 部分。

    【讨论】:

    • 谢谢,很有帮助
    猜你喜欢
    • 2023-03-26
    • 2013-10-08
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 2017-07-16
    • 1970-01-01
    相关资源
    最近更新 更多