2017-04-14 61 views
0

我使用.htaccess创建干净的网址。 这里是一片.htaccess文件的:干净的URL与.htaccess但只有一个网址

Options +FollowSymLinks -MultiViews 
RewriteEngine On 

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

RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L] 

RewriteRule ^posts/page/([0-9]+)/$ posts.php?page=$1 
RewriteRule ^posts posts.php 

它工作正常,但问题是这两个URL工作! 有没有办法只做site.com/posts/工作,并在site.com/posts.php上返回'not found'(或重定向到site.com/posts/)

想想我读了两处地址对于相同的页面对于seo不利。

回答

0

您可以创建在每一个“.PHP” URL返回404 HTTP代码重写规则:

RewriteRule .*\.php$ - [R=404] 
+0

now/posts/returns 404 too。无论如何,只有使/posts.php返回404? – theboy

0

这是对我工作的罚款。希望它对你也能很好地工作。

RewriteEngine On 
Options -Multiviews 

#For rewriting request to posts.php 
RewriteCond %{REQUEST_URI} ^/posts/?$ 
RewriteRule .* /posts.php [L,END] 

#For returning 404 on directly accessing /posts.php 
RewriteCond %{REQUEST_URI} ^/posts.php$ 
RewriteRule .* - [R=404,L] 
0

重定向/posts.php/职位如果你想在一部开拓创新的请求重定向到404,重写规则行改变这个

你可以使用这个

RewriteEngine on 

RewriteCond %{THE_REQUEST} /posts\.php [NC] 
RewriteRule^/posts [NE,L,R] 
#Your other rules 

RewriteRule^- [R=404,L] 
+0

我用它是这样的:RewriteCond%{THE_REQUEST} /*\.php [NC] RewriteRule^- [R = 404,L]它返回404,但它不显示我的404.php文件,它返回这个错误:此外,尝试使用ErrorDocument处理请求时遇到500内部服务器错误错误。 – theboy