I am developing a PHP website and I want to redirect php to html and I write the below code in htaccess file. Code is working fine and all the php pages are redirect to html but when I put file name with php in url site is also open with .
RewriteRule ^(.*)\.html$ $1.php [nc]
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[?\s] [NC]
RewriteRule ^ /%1.html [R=301,L]
RewriteRule ^(.+?)\.html$ /$1.php [L,NC]
Related
I have Some issue in .htaccess file.I have hosted my index page to webserver. I don't know what I did wrong in the .htaccess file.After adding code in .htaccess file still my url is not friendly.It is showing http://rang.comli.com/servicecnt It should be http://rang.comli.com/servicecnt/service_id/5
<html>
<head>
<title>Second mod_rewrite example</title>
</head>
<body>
<p>
welcome index page. Index page is loaded
Done with php my admin
</p>
</body>
</html>
Here is my .htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /public_html/index.php to /dir/index
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /public_html/index to /dir/index.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteEngine On
RewriteCond %{QUERY_STRING} ^service_id=([0-9]*)$
RewriteRule ^servicecnt\.php$ /servicecnt.php/%1? [R=302,L]
You can use:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+servicecnt\.php\?service_id=([^\s&]+) [NC]
RewriteRule ^ servicecnt/service_id/%1? [R=302,L]
## hide .php extension
# To externally redirect /public_html/index.php to /dir/index
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteRule ^servicecnt/service_id/(\d+)/?$ servicecnt.php?service_id=$1 [QSA,L]
## To internally redirect /public_html/index to /dir/index.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
Your best option is not to redirect the old url, but to let php generate the pretty url in the first place. Every time an user clicks on your link, the server needs to redirect the user, which is very inefficient. If the user would click the new url instead, you only have to internally rewrite it. You can use the following rule to rewrite it back:
RewriteRule ^servicecnt/service_id/([^/]+)/?$ servicecnt.php?service_id=$1 [L]
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 am using .htaccess file on my localhost and write this code:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^about/?$ about.php [NC,L]
I have checked http.doc file everthing is fine. But its not working fine.
Thanks.
You can put the .htaccess on the www folder and place the content on it:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*)\.php [NC]
RewriteRule ^ /%1.html? [R=302,L]
RewriteRule ^abc/about\.html/?$ /abc/about.php [NC,L]
I am stuck with .htaccess modification, need a little help.
First off, here is whats inside my htaccess.
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php [nc]
For example, I created the file named test.php and uploaded to my server.
I want my server to behave like this.
http://example.com/test/test.html -> http://example.com/test/test.html(as it is)
http://example.com/test/test.php -> http://example.com/test/test.html
but with the .htaccess I have right now,
I still have both .php and .html which may be considered as file duplication by search engine crawler like Google robot (isn't it?).
Any help appreciated.
try this .htaccess code
# Enable Rewrite Engine
RewriteEngine on
#Create friendly URL
RewriteRule ^test/(.*)\.html$ test/$1\.php [L]
OR
RewriteCond %{REQUEST_URI} ^(.*)\.php$
RewriteRule ^(.*) /$1.html [L]
OR
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## .php to .html
# To externally redirect /test/test.php to /test/test.html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1.html [R,L,NC]
This is a configuration file of apache '.htaccess' if you still want to configure then change
RewriteEngine on
I hope this work
You may try this in one .htaccess file in root directory:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !\.html [NC]
RewriteRule ^([^/]+)/([^.]+)\.php /$1/$2.html [R=301,NC,L]
For silent mapping, replace [R=301,NC,L] with [NC,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]