Htaccess redirect adds filepath to url - .htaccess

I'm trying to redirect my .html links to .php. The actual files have been renamed to php as well so my current .html links don't work. I tried this rewrite first which works as in it loads the page but doesn't change the url.
RewriteRule ^(.*)\.html$ $1.php [L]
But when I change the rewrite to redirect, it no longer works. It adds the file path to the url.
RewriteRule ^(.*)\.html$ $1.php [L,R=302]
For instance: www.domain.com/page.html redirects to www.domain.com/home/username/public_html/page.php
I've read many similar questions and tried their solutions but I haven't been able to solve the problem.

If you add slash before destination and rewritebase maybe it work:
RewriteEngine On
RewriteBase /
Rewrite file.html to file.php
RewriteRule ^(.*)\.html$ /$1.php [L]
301 Redirect to file.php
RewriteRule ^(.*)\.html$ /$1.php [R=301,L]

Related

Redirect in .htaccess doesn't work for url existing

I create a .htaccess to redirect all url to index.php
It works for url that doesn't exist but for other url (which exists) it doesn't work & I don't know why. Isearch & I tried all configuration but it seems it doest work.
Here my .htaccess :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [NC,L]
Anyone have a solution ?
This is because your RewriteConds does not allow existing Links and directories to go to /index.php . If you want to redirect all incoming requests (existing or non-existing) ,you can use the following :
RewriteEngine on
RewriteRule !index.php /index.php [L]
Also see this : Redirect all to index.php htaccess

Add .php extension with URLs Using .htaccess

I want to redirect my website URLs with .php extension currently all URLs are opening with and without .php
example
https://www.ranglerz.com/about.php
https://www.ranglerz.com/about
The both URLs are working now but I want if user open https://www.ranglerz.com/about it will redirect to https://www.ranglerz.com/about.php
here is my htaccess code
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ $1.php [NC,L]
I've tried all solutions that provided in different threads but nothing is working with this problem.
You can use:
Options -MultiViews
# Redirect to .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [R=301,L]

Formatting a search url

I'm rebuilding my site and want to change the urls - i need to let the search engines know so i'll be using 301 redirects.
my page is located at http://site.com/search.php
i want to use this url
http://site.com/search?s=foo&bar=bat&so+on&so+forth
my rewriteCond is
RewriteRule !^search search.php [R=301,L]
I'm getting a 404. What am I doing wrong?
Give this a try, it should remove the .php and redirect to search and show the content of the php:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*)\.php [NC]
RewriteRule ^ /%1 [R=301,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]
On the first rule we are checking the request to see if there is any .php present, if so we redirect it to the URL without the .php.
(.*) will capture anything before .php and the flag QSA will ensure the query string is appended to it.
^([^/]+)/?$ this will match anything not a / until we find a / or hit the end.
So if we access domain.com/index it will capture index and redirect to index.php internally.

Infinite loop error with .htaccess when I add 301 redirect with WordPress in subdirectory

I have my WordPress website in a subdirectory. I used the WordPress codex directions to set this up and all worked well until client asked me to add a 301 redirect to solve an issue with some links to an old domain on the Internet. The old domain name points to the new website but does not use a permanent 301 redirect. The current domain name is Encoreco.com.
The old domain points to the same place but it is not a 301 redirect (you can see that the URL does not change at the top and for some reason this means that my javascript slideshow doesn't work). Here is what happens: encoreconstructionco.com
When I add a 301 redirect to solve this problem, I get the infinite loop errors. Here is the line of code that attempts to solve the issue with the old domain but creates the infinite loop:
RewriteRule (.*) http://encoreco.com/$1 [R=301,L]
And here is my current .htaccess: Note - I tried taking the .html rewrite and the www rewrite out and that did not solve the problem.
RewriteCond %{THE_REQUEST} \.html
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule (.*) http://%1/$1 [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteRule (.*) http://encoreco.com/$1 [R=301,L]
… will redirect everything, including correct requests, so you have to get an infinite redirect.
Restrict that rule to wrong host names only:
RewriteCond %{HTTP_HOST} !encoreco\.com
RewriteRule (.*) http://encoreco.com/$1 [R=301,L]

redirecting files with html extension to files without extension (in url)

I have recently changed my website url using htaccess so that my urls will not show file extensions. Now my problem is as I have created a new xml sitemap so that my url will be extensionless!!! the Google webmaster tool is telling me about duplicate content issue!! ie. page and page.html have same title.... so my question is how do i redirect the urls with file extension html to urls with out extension!!!
this is an example of my website url with html extension
http://www.shenazhpeyk.co.uk/coding-machines.html
I want to redirect and change it to
http://www.shenazhpeyk.co.uk/coding-machines
so that will fix the issue with Google webmaster tools (Please provide me a code for use in htaccess file)
Many Thanks
Found this code as well. Not sure if it will accomplish the same thing. Seems to work for me as does the one above (for PHP).
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
I am wondering about the trailing slashes and whether those should be there or omitted?
Try adding the following to the .htaccess file in the root directory of your site redirect URLs with .html extension and remove it.
RewriteEngine on
RewriteBase /
#redirect to remove the .html extension
RewriteRule ^(.+)\.html$ $1 [L,NC,R=301]
Try this:
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]

Resources