2

I use URL rewriting on my redesigned website to give my pages tidier URLs.

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^$ index.php [L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^.+/$ %{REQUEST_URI}index.php [L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+\.php|(.+/)?index)$ - [R=404,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]

This .htaccess file allows /filename to actually point to /filename.php. It all works fine.

However, I have now realised that I should set up 301 permanent redirects, so that the pages of the old website (before the redesign) can redirect to pages on the new site (for SEO and linking reasons). The pages have been reorganised, so multiple old pages will redirect to new pages, for example.

The old website did not use URL rewriting. Therefore, I want to create permanent redirects such as /about-page.php to /about, doing them manually with one rule per old page.

I have tried several things, such as...

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^about-page.php$ about [R=301,L]

...or...

Redirect 301 /about-page.php /about

...but it always ends up either not working at all (giving me a 404 error when I attempt to access /old-filename.php, or breaks everything with internal server errors. It seems to work fine if I use Redirect 301 /about-page.html /about instead, but unfortunately the old URLs used .php extensions, not .html extensions.

I believe the problem is related to one of the other rules, which redirect requests for /xyz to /xyz.php, possibly creating some endless loop. But I can't figure out how to fix it.

Any advice? Thank you very much.


Edit: Final, working .htaccess file:

DirectoryIndex index.php #

RewriteEngine On

# -- Use a permanent redirect to point the old page to the new page.
# -- The RewriteCond is needed, or a redirect loop happens in certain cases.
# -- The full URL seems to be needed, or it redirects incorrectly.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^about-page.php$ http://www.mywebsite.com/about [R=301,L]

# -- Redirect most other .php files to a 404 error, to avoid duplicate content.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+\.php|(.+/)?index)$ - [R=404,L]

# -- Redirect requests without an extension, but for a valid file, to that file.
# -- I'm not sure what the RewriteCond lines are for, but they both seem necessary.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]
John Smith
  • 23
  • 4

1 Answers1

1
DirectoryIndex index.php # -- this sets index.php to be default file for a folder

RewriteEngine On

# -- RewriteRule ^(.+\.php|(.+/)?index)$ - [R=404,L]
# -- dude this above line redirects all php files to 404 error
# -- so delete this, its a problem not solution

RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]

RewriteRule ^about-page.php$ /about [R=301,L]

This should work, comment if problem occurs

Atul Gupta
  • 725
  • 1
  • 6
  • 20
  • Thanks for your response. Unfortunately, that doesn't seem to work. That makes "http://www.mywebsite.com/about-page.php" redirect to "http://www.mywebsite.com/home/mysite/mywebsite.com/about", which results in a 404 error. The rule to redirect PHP files to a 404 error was intended to prevent duplicate content at different URLs, by forcing users to access the pages at the rewritten URL (without the .php extensions); I didn't want them to access the real .php files directly. I think it would work fine if that rule could somehow be ignored when the 301 redirect takes place? – John Smith Sep 01 '11 at 21:33
  • Okay, I was successful. I was having a lot of trouble because my browser seemed to be caching the 301 redirect, and would continue to redirect even when I removed the rule from the .htaccess file! I also seemed to need the ENV:REDIRECT_STATUS RewriteCond line, although I'm not sure what it's for. I'll edit my question to include the final result. Thanks for the help. – John Smith Sep 02 '11 at 01:59
  • 1
    `RewriteRule ^about-page.php$ /about [R=301,L]` leading / prevent it from redirecting mywebsite.com/home/mysite/mywebsite.com/about – Atul Gupta Sep 02 '11 at 07:18
  • You can choose to redirect all your PHP files to non PHP addresses but redirecting them to 404 error is not good. 301 redirects are cached, so till you don't finalize your htaccess file, use 302 redirect, it isn't cached. – Atul Gupta Sep 02 '11 at 07:20