pages with .php extension redirect to .html - php

I m trying to redirect all pages with extension .php to .html
with the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)\.php$ /rules/$1.html [R=301,L]
RewriteRule ^([^\.]+)\.html$ /rules/$1.php [NC,L]
but got an error "This webpage has a redirect loop"

You have done first redirection of .php to .html and after that you doing .html to .php redirect so, that will recursion of redirection.
make code like this. that will redirect .php to .html files.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)\.php$ /rules/$1.html [R=301,L]

Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo.html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1.html? [R=301,L]
# To internally forward /dir/foo.html to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)\.html?$ /$1.php [L,NC]

Related

removing .html or .PHP URL extension using .htaccess not working

can anyone help me to remove .html or .php extension in URL.
I am trying this below code in my php project, but it is not working.
I have tried so many .htaccess codes, but nothing helps me..
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.php
Thanks in advance
Previously I also faced problem with removing .html or .php extension form URL.
I have a Solution Here
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
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]
put the above code in .htaccess file, above code is for .php files.
for .html files go through the below code
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]
I just replaced the .php with .html
Hope it helps You.. :)

Redirect loop error

I have hide .php extension of all the pages using .htaccess
But when some one types link address with .php extension the page still opens up with .php extension. I have tried permanent redirect .php page to non php page for example example.com/about.php to example.com/about but its giving me redirect loop error.
Here is my .htaccess code
RewriteEngine on
<IfModule mod_rewrite.c>
RewriteRule ^products/(\d+)/([\d\w-\s]*)/([\d\w-\s]*)/([\d\w-\s]*)/?$ product_detail.php?id=$1&category=$3&parent_category=$2 [L,QSA]
</IfModule>
Options +MultiViews
ErrorDocument 404 /404.html
Keep your code like this in root .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
RewriteRule ^products/(\d+)/([\d\w\s-]*)/([\d\w\s-]*)/([\d\w\s-]*)/?$ product_detail.php?id=$1&category=$3&parent_category=$2 [L,QSA]
You can try adding this to the top of your rules (just under RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+(.*)\.php
RewriteRule ^ /%1 [L,R=301]

301 redirect .php to non-php page

Currently, my site is set up to redirect a .php to a non-php version. Problem with this is when I checked, it is issuing a "200 OK" for both pages, which I know is a no no with google.
I want to 301 redirect the .php page to the non-php. It is the same page, and I have already added the canonical link to the page, reflecting the non-.php page.
How do I set up the 301? I am currently handling the .php to direct to non-.php page using .htaccess with the following code:
# Remove filename extension
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [QSA,NC,L]
</IfModule>
Any ideas how to resolve the 200 OK to 301 redirect the .php page (note they are the same page)?
You need one additional rule:
# Remove filename extension
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# To externally redirect /dir/abc.php to /dir/abc
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [QSA,NC,L]
</IfModule>
Place this rule before your existing rule.
This will externally redirect all .php file to non .php version. Thus Google will not index your .php file.

PHP .htaccess URL Rewriting [duplicate]

This question already has an answer here:
htaccess rewrite condition not working
(1 answer)
Closed 8 years ago.
Basically I'm a .net developer so I have no clue regarding this.
I have purchased a php script[site] and configured it over http://spicyindia.net/
eveything works fine when I append php to the file extension.
for eg works fine
http://spicyindia.net/all.php
but
http://spicyindia.net/all
fails
by some RND which I did there is a file named .htaccess which is responsible for this operations..
I have that file uploaded into hosting server too.
can some one tell me if there is any extra configuration which is needed for the same.
some lines in .htaccess .
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^login$ login.php
RewriteRule ^logout$ logout.php
any help would be appreciated.
In your .htaccess file, try this to access all,
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
It rewrites only login and logout
Try using this one:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
It rewrites every .php file
This will convert all www.domain.com/xyz.php to www.domain.com/xyz
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
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]
If you want to hide .php extension then remove those 2 RewriteRule and have your .htaccess like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}/+\s(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
Reference: Apache mod_rewrite Introduction

url rewrite to short url

Good days guys, I need your help with this url rewrite.
The are two rewrites I want to do.
1) I want to take out .php from the url even though my files is saved as .php
2) I want to rewrite the url below
If you can help me. It will be lovely. Thanks
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

Categories