dqzpt40064 2014-02-04 11:26
浏览 63
已采纳

htaccess将url页面重写为文件夹

I'm trying to make a .htaccess page that can change my url from www.site.com/about.php to /About/. All the pages are in the root currently so I'll want to do this for multiple pages. So far I have this in my htaccess file:

RewriteEngine On  

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

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

This seems to just change all pages other than the index.php to the about page. My hyperlinks in the nav now go to /About/, /Contact/ etc but I can't work out what I need to change to make the links that aren't about stop showing the about.php.

Any help would be great with this as I'm very new to PHP. Thanks in advance for any guidance

  • 写回答

1条回答 默认 最新

  • dongpeng7744 2014-02-04 11:33
    关注

    You are matching ALL requests with

    ^(.*)$
    

    and sending them to 'about.php'.

    What you want to do is match the request, then use that match to construct the file name:

    # 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]
    

    This is a very basic way to do it. You probably want to enhance the rule so it only match on the first URL part.

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么