dqask02082 2014-05-07 23:10
浏览 37

将php文件重写为文件夹,而不是使用名为/ about的文件夹

My url right now is:


Result wanted: localhost/about.php But I want it to appear as localhost/about/ without having to place it into an about folder.

I'm quite inexperienced on how you would do this with a .htaccess file.

I found this idea online, but mine isn't using a database:

Example 3: The site has an unwieldy URL. How to clean it up This sort of situation can arise when URLs are long and complex.

Take the URL below as an example:


As effective as the URL is in delivering the correct content, it is not very memorable for the consumer. URL rewrites would allow you to convert the URL to something simpler and clearer:


In order to accomplish this, we would need the following lines in our .htaccess file (you can add as many section as needed in the .htaccess file):

RewriteEngine on
RewriteRule ^(meat|produce|dairy)/([^/.]+)/([^/.]+)$ results.php?products=$1&type=$2&species=$3

Now for an explanation:

First the ^(caret) starts the expression.

(meat|produce|dairy): If we want to limit the options that can be typed in, we can specify the only values we will accept: in this case the variety of groceries. If anything besides one of those three 3 keywords is typed in, the URL rewrite will not take place.

The ([^/.]+) indicates that anything can be written between the forward slash besides the characters following the caret, in this case, the forward slash or period.

results.php?products=$1&type=$2&species=$3: Each value in the parentheses will be extracted and then applied to the longer URL in the substitution part of the expression. $1 indicates the first parantheses, $2, the second, $3, the third.

  • 写回答

1条回答 默认 最新

  • dongquweng5152 2014-05-07 23:13

    To remove the .php from the URL you can use this...

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*?)/?$ $1.php [L]

    just save it as a file name .htaccess in the root of your application.

    This will allow you to use localhost/about as well as localhost/about.php both are valid.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 怎么实现只排序每行中的第二列的数 如果比两数都大就放在第三列中 比两数都小就放在第一列中(语言-c语言)
  • ¥15 应用商店如何检测在架应用内容是否违规?
  • ¥15 Ubuntu系统配置PX4
  • ¥20 MATLAB间接平差计算
  • ¥50 nw.js调用activex
  • ¥15 数据库获取信息反馈出错,直接查询了ref字段并且还使用了User文档的_id而不是自己的
  • ¥15 将安全信息用到以下对象时发生以下错误:c:dumpstack.log.tmp 另一个程序正在使用此文件,因此无法访问
  • ¥15 速度位置规划实现精确定位的问题
  • ¥15 MAC虚拟机(win11)USB插上后无串口com,无法烧录
  • ¥15 代码问题:df = pd.read_excel('c:\User\18343\Desktop\wpsdata.xlxs')路径读不到