2015-04-17 71 views
0

我有一个现有的网站与PHP中的索引的URL。我想更改网址以不包含PHP,因此thepage.php将被重写为http://example.com/thepagehtaccess重写URL重定向导致循环

重写正在工作,但当我尝试访问时,重定向导致'页面无法在循环中显示'错误该页面使用page.php

我需要弄清楚如何设置重定向并重写URL,因为Google现在已经将PHP URL和漂亮的URL编入索引。

我看了无数网页,但无法找到答案。

请帮忙。

这里是我的htaccess

# The redirect below caused a loop when I access the page as 
# http://example.com/thepage.php 
# Moving the redirect after the rewrite didn't work either. 

Redirect 301 /thepage.php http://example.com/thepage 

RewriteEngine on 
# Rewrite works. The page does display corectly for this link 
# http://example.com/thepage 

<IfModule mod_rewrite.c> 
    Options +FollowSymLinks 
    Options +Indexes 
# RewriteEngine On 
    RewriteCond %{SCRIPT_FILENAME} !-d 
#RewriteRule ^photos([^\.]+)$ photos [NC,L] 
    RewriteRule ^([^\.]+)$ $1.php [NC,L] 
</IfModule> 


ErrorDocument 404 http://example.com/ 

谢谢

回答

0

你重写规则,从它的声音是落后的。

这里我们捕获所有不包含句点的php文件,并只使用没有扩展名的名称将它们重定向到目录URI。 [R=301]是一个永久的301重定向,告诉谁不再在旧的地方。

RewriteEngine on 
RewriteRule ^([^\.]+).php$ $1 [NC,L,R=301] 

您可能希望只是没了301 R来检验这些以确保它的工作。如果您执行的301重定向不起作用,它将被缓存到最终用户的浏览器缓存中,直到它到期。

Apache有一个extensive reference on remapping URLs with redirects

+0

谢谢组合。当我这样做,我得到404错误,除了主页 \t RewriteEngine叙述上的所有网页 \t \t选项+的FollowSymLinks \t选项+指标 \t的RewriteCond%{} SCRIPT_FILENAME!-d \t重写规则^([^ \。] +)。PHP的$ $ 1 [NC,L,R = 301] \t \t #ErrorDocument 404 http://example.com/ – rcdev

0

我终于能够回到这个问题,并找到解决方案。我希望这可以帮助别人。我在这个问题上撕裂了我的头发。

我发现很多例子,但不是我需要

#Force non-www: 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^www.example.com [NC] 
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301,NC] 

RewriteBase/

# hide .php extension 
# To externally redirect /dir/foo.php to /dir/foo 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+).php [NC] 
RewriteRule^%1 [R,L,NC] 

# To internally redirect /dir/foo to /dir/foo.php 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule^%{REQUEST_URI}.php [L] 

ErrorDocument 404 http://example.com/