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]
Related
So I've been trying to rewrite my php URLs with .htaccess so that they are more SEO/user friendly. Here is my code:
RewriteBase /
Options +FollowSymLinks -MultiViews
DirectorySlash On
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*)/$ /?lang=$1 [L]
RewriteRule ^([^/]*)/contact$ /contact?lang=$1 [L]
What this does is, it turns this:
http://www.example.com/?lang=en
http://www.example.com/contact?lang=en
Into this:
http://www.example.com/en/
http://www.example.com/en/contact
The problem:
http://www.example.com/en/
is not the same thing as:
http://www.example.com/en
And I get a 404 Not Found error when I try to point my browser's address to http://www.example.com/en. How can I make both versions of this work?
Adding DirectorySlashes On to my root .htaccess did not work. But my mobile version located in the /m/ folder has the following .htaccess:
# Set dir index
DirectoryIndex index.php
# Set dir slashes
DirectorySlash On
And it works! When I visit http://www.example.com/m I get redirected to http://www.example.com/m/. That is the wanted behavior. I guess it works because /m/ is a sub directory...
The question:
How can I make http://www.example.com/en redirect to http://www.example.com/en/ without breaking the rest of my URLs?
Edit (my entire .htaccess):
# Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working
RewriteBase /
# Disable directory views
Options All -Indexes
IndexIgnore *
# Follow Symbolic links
Options +FollowSymLinks -MultiViews
# Create Directory Slashes
DirectorySlash On
# Rewrite urls
<IfModule mod_rewrite.c>
RewriteEngine on
# Pretty desktop URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*)/$ /?lang=$1 [L]
RewriteRule ^([^/]*)/prices$ /prices?lang=$1 [L]
RewriteRule ^([^/]*)/offers$ /offers?lang=$1 [L]
RewriteRule ^([^/]*)/maps$ /maps?lang=$1 [L]
RewriteRule ^([^/]*)/contact$ /contact?lang=$1 [L]
RewriteRule ^([^/]*)/links$ /links?lang=$1 [L]
RewriteRule ^([^/]*)/error$ /error?lang=$1 [L]
RewriteRule ^([^/]*)/verify-new$ /verify-new?lang=$1 [L]
RewriteRule ^([^/]*)/verify-old$ /verify-old?lang=$1 [L]
RewriteRule ^en/ski-rent-prices$ /ski-rent-prices [L]
# Rewrite mobile URLs
RewriteRule ^([^/]*)/m/$ /m/?lang=$1 [L]
# WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Rewrite URL without file extensions
# For .PHP files
RewriteRule ^error$ error.php [L]
RewriteRule ^offers$ offers.php [L]
RewriteRule ^prices$ prices.php [L]
RewriteRule ^maps$ maps.php [L]
RewriteRule ^contact$ contact.php [L]
RewriteRule ^links$ links.php [L]
RewriteRule ^verify-new$ verify-new.php [L]
RewriteRule ^verify-old$ verify-old.php [L]
# For .PDF files
RewriteRule ^ski-rent-prices$ ski-rent-prices.pdf [L]
</IfModule>
# Add Custom Error pages
ErrorDocument 400 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php
# Default Character set
IndexOptions Charset=UTF-8
AddDefaultCharset UTF-8
# Hide the server signature
ServerSignature Off
<IfModule mod_rewrite.c>
# Add correct content types for documents
# JS:
AddType text/javascript .js
# CSS:
AddType text/css .js
# TXT:
AddType text/plain .txt
</IfModule>
<IfModule mod_headers.c>
# IE Compability Mode
BrowserMatch (MSIE|Trident) ie
Header set X-UA-Compatible "IE=Edge,chrome=1" env=ie
# Disallow 3rd party iframes
Header always append X-Frame-Options SAMEORIGIN
</IfModule>
Rewrite --- www.example.com/?lang=en => www.example.com/en/
RewriteCond %{QUERY_STRING} (^|&)lang=en($|&)
RewriteRule ^$ /en/? [L,R]
Updated code for testing
RewriteBase /
Options +FollowSymLinks -MultiViews
DirectorySlash On
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]
RewriteCond %{REQUEST_URI} !^(.+)/(.+)/$
RewriteRule ^(.+)/ ?lang=$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^(.+)/(.+)/m/$
RewriteRule ^(.+)/(.+)/ $2?lang=$1 [L,R=301]
RewriteRule ^(.+)/(.+)/m/ $2/m/?lang=$1 [L,R=301]
See the browser address bar and check if the redirections are correct. If it wrong provide the source link and redirected link. If it is ok, Then use the below code.
RewriteBase /
Options +FollowSymLinks -MultiViews
DirectorySlash On
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]
RewriteCond %{REQUEST_URI} !^(.+)/(.+)/$
RewriteRule ^(.+)/ ?lang=$1 [L]
RewriteCond %{REQUEST_URI} !^(.+)/(.+)/m/$
RewriteRule ^(.+)/(.+)/ $2?lang=$1 [L]
RewriteRule ^(.+)/(.+)/m/ $2/m/?lang=$1 [L]
I have tried the code with the below links and its working fine.
http://www.example.com/en
http://www.example.com/en/
http://www.example.com/en/contact
http://www.example.com/en/contact/
http://www.example.com/en/contact/m
http://www.example.com/en/contact/m/
http://www.example.com/en/prices
http://www.example.com/en/prices/
http://www.example.com/en/prices/m
http://www.example.com/en/prices/m/
Check these links yourself and also other links as well.
New .htaccess file (still not working, it sends me to localhost/xampp
RewriteEngine on
RewriteBase /sample/
# set root to index.php
DirectoryIndex index.php
# prevent directory listing
Options -Indexes
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index.php$ $1 [R=301,L,NC]
# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]
Old .htaccess file below:
RewriteEngine on
# set root to index.php
DirectoryIndex index.php
# prevent directory listing
Options -Indexes
# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# remove index.php
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://localhost/sample/ [R=301,L]
Domain is http://localhost/sample/. I have tried many things that Google has given me but none of them work. Every time I go to localhost/sample/index.php it doesn't redirect to localhost/sample/. I also used the Redirect 301 /index.php /.
I tried
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
But it sends me to http://localhost/xampp/splash.php.
I am still new to .htaccess so I really don't have any idea what I am doing wrong. I've tried the answers that have been given to the questions where the OP said the answer worked.
You can use:
# set root to index.php
DirectoryIndex index.php
# prevent directory listing
Options -Indexes
RewriteEngine on
RewriteBase /sample/
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index.php$ $1 [R=301,L,NC]
# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]
RewriteBase /sample/ is needed to make sure redirect happens from raltive path of current directory.
Use this:
RewriteEngine On
# prevent directory listing
Options -Indexes
# allows leaving off the extension (.php) in urls
Options +MultiViews
# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^index(\.php)?$ http://localhost/sample [R=301,L]
I am trying to do a url rewrite for php dynamic pages. The rewrite sends them to the page but I am getting a 404 error. In my php page i use $coinshow =$_REQUEST['coinshow'];
I want to to go from sitename.com/coinShowPage.php?coinshow=XyzShow to sitename.com/XyzShow or I would be happy with sitename.com/coinshows/XyzShow too.
Please note coinShowPage.php is the only page I need redirected.
Here is the .htaccess file
RewriteEngine On<br>
RewriteCond %{THE_REQUEST} ^[A-Za-z]{3,}\s/+coinShowPage\.php\?coinshow=([^\s]+) [NC]
RewriteRule ^ http://www.SiteName.com/%1? [R=301,L]
I already tried the following and it did not work
RewriteEngine On
Options +FollowSymlinks -MultiViews -Indexes
RewriteBase /
RewriteRule ^/([0-9]+)/?$ coinShowPage.php?coinshow=$1 [NC,L]
Any help would be greatly appreciated.
Try this:
Options +FollowSymlinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Za-z]{3,}\s/+coinShowPage\.php\?coinshow=([^\s]+) [NC]
RewriteRule ^ http://www.SiteName.com/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ coinShowPage.php?coinshow=$1 [QSA,L]
Options -Indexes
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_FILENAME}.html [L]
In my website I am using this .htaccess code to remove .html extensions from url. It works fine in some urls but it gives 403 forbidden error in some urls. I checked file permissions of these urls. Those are ok 0644.
Answer:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Per Trouble removing .html URL extension using .htaccess
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]