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]
Related
I want to remove .php extensions from all files. For example if the user puts example.com/test.php I want to to go to example.com/test and show the page content of test.php
But I want the htaccess file to to this function only on ONE url. Not on all the add-on domains I have
How can I do this?
You may use this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(test)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(test)/?$ $1.php [L]
I have an issue with new installed centos web panel where I am using below htaccess code to redirect to index.php if file not exists where if estate.php exists then it goes directly to estate.php and if i use estate without .php then it goes to index.php.
This code below works anywhere but not on CWP (centos web panel) and the problem is that it still works for the files that don't exist at all but if file estate.php exist and in url i use estate without .php it goes to esate.php and ignors to go to index.php.
so the question is that is there any way to force it to go to index.php even if file exists but if i use estate.php then it should go directly to estate.php.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?path=$1 [NC,L,QSA]
</IfModule>
You need to disable the Multiviews
Try adding the following line in your htaccess above RewriteEngine directive :
Options -Multiviews
I got a .htaccess that pretty much looks like this:
# disable directory browsing
Options All -Indexes
# start rewrites
RewriteEngine on
Redirect 301 /someoldpage.php http://example.com/fancyurl
# if not a file, and not a directory, reroute through index as normal page
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]
What I want to accomplish is that the url is rewritten also when someone is trying to access an old 301 redirectred url, so when going to http://example.com//someoldpage.php you see the url http://example.com/fancyurl and NOT like now: http://example.com/index.php?page=fancyurl
Redirect is part of mod_alias, while the rewrite stuff is part of mod_rewrite. The two different modules both get applied to the same request and thus causing it to redirect and rewrite at the same time. What you want to do is have it redirect if the request is /someoldpage.pjp and not rewrite to index.php, so you need to use only mod_rewrite here:
# disable directory browsing
Options All -Indexes
# start rewrites
RewriteEngine on
RewriteRule ^someoldpage\.php$ http://example.com/fancyurl [L,R=301]
# if not a file, and not a directory, reroute through index as normal page
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]
I Struck with this problem and got a solution in my case, to rewrite the url from example.com/about.php to example.com/about with the following code in your htaccess.
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_FILENAME}.php [L]
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]
I originally wanted to have all my urls end with no extension. Unfortunately, I've tried many htaccess codes and I've just about given up.
So now I want to make it so if a person wants to visit a page in my site, but forgets to enter .php, he/she will automatically be redirected to the same url but with the .php
How can this be done? Thanks!
Here are the rules:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
It will check if requested resource is not a existing folder. For example: you requesting http://www.example.com/help. If there is such folder present (/help) the rule will do nothing (priority is given to a folder). If you do not want this behaviour then remove the first line.
It will check if there is such .php file before rewriting. For example: you requesting http://www.example.com/aboutus but there is NO aboutus.php file there -- no rewrite will occur.
All such requests should be without trailing slash: should be http://www.example.com/aboutus and NOT http://www.example.com/aboutus/
The rule will work for URL in subfolders as well: e.g. http://www.example.com/pages/help/aboutus will be rewritten just fine.
Because of the above checks the rule will not enter into a rewrite loop (no 500 error on this rule)
Query string (page parameters) will be preserved
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]
In response to Sun Love, the code you posted works well except for situations where you have a trailing slash without the file extension (I get a 500 error) because the first RegEx doesn't match for this situation.
example.com/test.html -- works (redirects to /test)
example.com/test -- works (no redirect)
example.com/test.html -- works (redirects to /test)
example.com/test/ -- doesn't work (500 error)
There is probably a better way to do this but I added another rewrite condition to fix this:
RewriteEngine on
Options +FollowSymLinks -MultiViews
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+).php
RewriteRule ^ %1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)/\s
RewriteRule ^ %1 [R=301,L]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
Try this .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([^\.]+)$ $1.php [L]
EDIT: ^([^\.]+)$ $1.php [L]