简体   繁体   English

htaccess将网址页面重写到文件夹

[英]htaccess rewrite url pages to folders

I'm trying to make a .htaccess page that can change my url from www.site.com/about.php to /About/. 我正在尝试制作一个.htaccess页面,该页面可以将我的URL从www.site.com/about.php更改为/ 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: 到目前为止,我的htaccess文件中包含以下内容:

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. 这似乎只是将除index.php以外的所有页面更改为About页面。 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. 我在导航中的超链接现在转到/ About /,/ Contact /等,但是我无法确定需要进行哪些更改以使那些不打算停止显示about.php的链接。

Any help would be great with this as I'm very new to PHP. 有了这个,任何帮助都会很棒,因为我对PHP还是很陌生。 Thanks in advance for any guidance 在此先感谢您的指导

You are matching ALL requests with 您正在将所有请求与

^(.*)$

and sending them to 'about.php'. 并将它们发送到“ 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. 您可能想增强规则,使其仅在第一个URL部分匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM