.htaccess Rewrite URL with redirect causes loop

I have an existing site with php in the URL indexed. I want to change the URLs to not include PHP so thepage.php will be rewritten to http://example.com/thepage

The rewrite is working, but the redirect causes a ‘page can’t be displayed in a loop’ error when I try to access the page as using thepage.php

I need to figure out how to setup the redirect and also rewrite the URLs, because Google has now indexed the PHP URLs and the pretty URLs.

I’ve looked at countless pages online, but can’t find the answer.

Please help.

Thank You

Here’s my 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/

RC,

I answered that as “you can’t get there from here” years ago but I then came up with two solutions - both of which are covered in my tutorial at http://dk.co.nz/seo - look for the “redirect to new format” in the dropdown page navigation aid (this is a LONG page to make it easier for members to get all the info in one place).

The first method created a “marker” to tell mod_rewrite that it had been redirected once (and not to redirect again) while the second method used an Apache variable which does the same thing. There is sample code for both versions in the tutorial (which has been removed from the “sticky threads” when SitePoint went to this forum software - which I still don’t like; my problem, though, isn’t it?).

THEN, I have traditionally hammered on those noobie webmasters who did not know whether mod_rewrite was enabled or not (i.e., left the test in to be repeated ad nauseum). In other words, understand the code you’re using, please, because you don’t need to LOOK like a noobie.

Regards,

DK

Thanks for your reply. I am an experienced PHP and database developer, but I’m not an expert when it comes to htaccess.

I’m under stress to get this done quickly which makes it harder to absorb.

I visited the page and tried a few things that didn’t work.

I will take another look a bit later with “new eyes”.

Thanks again.

RC,

Great … except that you should know better with your experience.

If you have knowledge of regular expressions, the regex used in mod_rewrite statements is a simple subset (no formatting and single lines) so mod_rewrite should come easily to you. Stress, on the other hand, means that a good night’s sleep (or just a shower always helped me) will clear the head and give an opportunity to see the problem just a bit differently.

The code within my tutorial (redirect TO new format) works - both of them! It should be a snap for you to alter the code to fit your new format (remove file extensions). If you’re finding that you’re getting 404’s, be sure that the PHP file exists (-f) before making the redirection (because a 404 handler script may be more appropriate). Also, check the “extensionless filenames” section for other hints.

Finally, the best friend during troubleshooting is the R=301 flag as it will display the ultimate redirection to show whether the mod_rewrite code is working or not.

Regards,

DK

I finally was able to get back to this problem and found the solution. I hope this can help somebody else. I tore my hair out on this problem.

I found many examples but not with the combination that I needed

#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} [1]{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/


  1. A-Z ↩︎

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.