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.

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

报告相同问题?