重写URL后,禁止使用文件扩展名访问页面 [英] After URL rewrite, prevent accessing page using file extension
问题描述
我正在为我的一个网站进行.htaccess开发。到目前为止,这是我的代码:
RewriteEngine On
#省略www。
RewriteCond%{HTTP_HOST} ^ www\.website\.com [NC]
RewriteRule ^(。*)$ http://website.com/$1 [L,R = 301]
#现在从页面名称
省略了扩展名。
RewriteCond%{REQUEST_FILENAME}!-f
RewriteRule ^([[^ \。] +)$ $ 1.php [NC, L]
这一切都很好,网站在 www除外。
和所有 .php
扩展名都被删除。我还从标记中的页面链接中删除了 .php
扩展名,同样,这些页面也可以按我的意愿提供,例如,什么是 website.com/news.php
现在显示为 website.com/news
很酷。
我接下来意识到,仍然可以通过输入带有文件扩展名的页面名称来访问页面,因此 website.com/news.php
在地址栏中显示扩展名为 website.com/news.php
是否可以防止这种情况?因此,如果在地址栏中输入 website.com/news.php
,浏览器将提供 website.com/news
?
如果
website.com/news.php
输入到地址栏中,浏览器将提供website.com/news
:
当然,您可以添加重定向规则以删除 .php
:
RewriteEngine On
#省略www。
RewriteCond%{HTTP_HOST} ^ www\.website\.com [NC]
RewriteRule ^(。*)$ http://website.com/$1 [L,R = 301,NE ]
#要将/dir/file.php外部重定向到/ dir / file
RewriteCond%{THE_REQUEST} \s /+(.+?)\.php [\ s?] [NC]
RewriteRule ^ /%1 [R = 302,NE,L]
#在内部将/ dir / file重定向到/dir/file.php
RewriteCond%{REQUEST_FILENAME}!-d
RewriteCond%{REQUEST_FILENAME} .php -f
RewriteRule ^(。+?)/?$ $ 1.php [L]
I'm working on .htaccess development for one of my websites. So far, here is my code:
RewriteEngine On
# omit www.
RewriteCond %{HTTP_HOST} ^www\.website\.com [NC]
RewriteRule ^(.*)$ http://website.com/$1 [L,R=301]
# now omit.php extension from page names
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This all works fine, the site is served up sans www.
and all the .php
extensions are removed. I also removed the .php
extensions from my page links in the markup, and again, the pages are served up as I want, for example, what was website.com/news.php
now shows as website.com/news
Cool.
What I next realized was that the pages were still able to be accessed by typing in the page name with the file extension, so website.com/news.php
pulls up the page with the file extension in the address bar as website.com/news.php
Is there a way to prevent this? So that if website.com/news.php
is entered into the address bar, the browser will serve up website.com/news
?
if
website.com/news.php
is entered into the address bar, the browser will serve upwebsite.com/news
:
Sure you can add a redirect rule to remove .php
:
RewriteEngine On
# omit www.
RewriteCond %{HTTP_HOST} ^www\.website\.com [NC]
RewriteRule ^(.*)$ http://website.com/$1 [L,R=301,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]
# To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
这篇关于重写URL后,禁止使用文件扩展名访问页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!