Remove .html from URLs with a redirect - apache

We have a website, unfortunately all the URLs have the .html suffix, its a Magento installation, Magento allows you to change this on the CMS, but again, unfortunately all this URLs with .html suffix have a good ranking in Google. We need to redirect to non .html.
So, consider the following scenario, we are rebuilding this site from scratch, so we have the same urls on the new site but without the .html suffix.
Now is: www.example.de/cool-shoes.html
Will be: www.example.de/cool-shoes
So www.example.de/cool-shoes.html will not exist anymore, and I've been trying a redirect with the .htaccess with no luck.
I've tried so far:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule (.*)index\.html$ /$1 [R=301,L]
and:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
but it doesn't seem to work...any ideas?

Ok so, after some research, and failing to achieve this with a rewrite rule, the following line of code worked:
redirectMatch 301 ^(.*)\.html $1
This is quite usefull to remove any url extension and avoid broken links, hopefully helps someone in the future...
cheers!

This will rewrite the url like so http://example.com/page.html -> http://example.com/page
# Remove .html from url
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

Try adding the following to the .htaccess file in the root directory of your site redirect URLs with .html extension and remove it.
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]

Here's the solution that worked for me.
RewriteCond %{THE_REQUEST} \.html [NC]
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]

Follow the steps, and you'll be able to remove .html from url without modifying .htaccess file.

This should do the trick:
RewriteEngine On
RewriteRule ^(\w+)\.html$ /$1 [R=301,L]

Try this to putting in your .htaccess file
Redirect permanent www.mysite.de/cool-shoes.html
www.mysite.de/cool-shoes
this may be helpful to you

This is for URLs ending with .html /product/raspberrypi.html ---> /product/raspberrypi/ (/product/raspberrypi/index.php) the index.php is hidden. Took me awhile to figure this out. LOL...
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
You have to use 'REQUEST_URI' and add it before index redirect rules since it could be overridden by the application. Its important to know that its URI not a filename or directory we are trying to redirect, since the file names all have index.php in the root folders(Wordpress).

Related

Mod Re-Write Struggling With Rule Not Working

So after a lot of testing - I've started to figure out my problems but still can't get the rewrite to work.
This is how my htaccess file looks now.
RewriteEngine On
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://abfielder.com/$1 [R=301,L]
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
Rule 1 is the one I'm having problems with
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
My understanding is now if I type this url into my browser
https://abfielder.com/schemdetail/id/158
I should get
https://abfielder.com/schematicdetails?id=158
The test of this here
https://htaccess.madewithlove.com/
Tells me my rules are ok.
And rules 2 and 3 are working fine.
However when I try and access
https://abfielder.com/schemdetail/id/158
I essentially get the page not found error.
Ok think I've finally fixed this for anyone else who is having issues with mod rewrite where they wish to have a rule to remove the file extension and other rewrites working together the order matters. Lastly if you're on a shared host type thing you may also need to set the rewrite base. This is how my htaccess file looks now.
RewriteEngine On
RewriteBase /
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://abfielder.com/$1 [R=301,L]
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
This allows me to have url's without the .php extension.
It also allows for
schemdetail/id/100 to be translated into schematicdetails?id=100

Long URL in php using htaccess

I have a very long URL: //domain/administration/registrar/term_detail.php?a_id=47
And I want to make it looks like: //domain/administration/registrar/detail/47
I have created an htaccess file in //domain/administration/registrar/. Below is my code in htaccess and it is not working. Any suggestion of what I did wrong is appreciated.
Both of my htaccess and term_detail.php files are inside of registrar folder.
RewriteEngine On
RewriteBase /administration/registrar/
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} \s/+registrar/detail\.php\?a_id=([^\s&]+) [NC]
RewriteRule ^ administration/registrar/%1? [R=301,L]
RewriteRule ^administration/registrar/([a-zA-Z0-9]+)/?$ detail?a_id=$1 [L,QSA,NC]
You can try these rules inside your /administration/registrar/.htaccess:
RewriteEngine On
RewriteBase /administration/registrar/
## External redirect to /administration/registrar/term_detail/47
RewriteCond %{THE_REQUEST} /([^./]+)(?:\.php)?\?a_id=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L,NE]
## Internal rewrite to term_detail.php?a_id=47
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?a_id=$2 [QSA,L]
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
Keep your htaccess file inside registrar folder.
RewriteEngine ON
##External redirect to //domain/administration/registrar/detail/47 URL.
RewriteBase /administration/registrar/
RewriteCond %{THE_REQUEST} \s/([^/]*)/([^/]*)/(?:[^_]*)_([^.]*)\.php\?a_id=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=301,L]
##Internal rewrite to term_detail.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ $1/$2/term_$3.php?a_id=$4 [QSA,L]

How to remove and avoid url directories in htaccess

I'm trying to allow my site to rewrite urls. I have put the following into my .htaccess file in the root directory.
RewriteEngine On
#would be nice to remove member-pages from the URL but no idea how.
#RewriteRule ^members/(.*)/?$ /$1 [NC,R]
#This part works though!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ ./members/$1/ [L]
So far, it takes
mydomain.com/someUserName or mydomain.com/someUserName/ (with trailing slash) and, if it exists, will load the page at mydomain.com/members/someUserName/ without a hitch. This works like a gem.
What I want now (and am trying to do with the first rewrite rule) is to take a mydomain.com/members/someUserName or mydomain.com/members/someUserName/ and have it show up as mydomain.com/someUserName in the url.
How do I do this? Thanks in advance!
If I understand you correctly, You want to redirect domain.com/members/foo to domain.com/foo , You can use the following rule for that:
RewriteEngine On
RewriteCond %{THE_REQUEST} /memebers/([^\s]+) [NC]
RewriteRule ^ /%1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ ./members/$1 [NC,L]

mod_rewrite redirect url if no file found to main page

I want to be able to Access my scripts without the .php, .html etc., so I already wrote
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php
##same for other extensions
in my .htaccess file (note: this file lies not in the root-path), but I also want to Redirect every incorrect request to my main page, so that www.mysite.com/dir/incorrect will be rewritten to www.mysite.com/dir/.
But my first try (RewriteRule ^ / [R] after RewriteCond) redirected me to www.mysite.com/, my experiments with RewriteBase (RewriteBase . and RewriteBase /) didnt work and I also noticed that many similar scriptredirect to www.mysite.com/dir/index.php (www.mysite.com/dir/index in my case), but I really want to Redirect to www.mysite.com/dir/. Is there any way to achieve this?
Have it this way:
RewriteEngine on
# see if .php is found
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php [L]
# determine DIR_BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=DIR_BASE:%1]
# if not found redirect to %{ENV:DIR_BASE}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:DIR_BASE} [L,R]

.htaccess redirect and add variables

our clients banner campain lauched today an the wrong url has been deposited.
So i try to redirect
website.com/wheels/?lang=en&sel=540
to
website.com/wheels/index.html?lang=en&sel=540
I tryed to put a .htaccess in the folder wheels which contains following:
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)/?$ index.html?lang=$1&sel=$2
But it doesn't work. Anyone has an idea, what i'm doing wrong?
EDIT
I found a existing .htaccess in the ROOT. It contains following code:
RewriteEngine On
RewriteBase /
RewriteRule ^(typo3|typo3temp|typo3conf|t3lib|tslib|fileadmin|uploads|showpic\.php)/ - [L]
RewriteRule ^typo3$ typo3/index_re.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* /index.php [L]
If I add the code folling code, nothing happens:
RewriteCond %{QUERY_STRING} ^lang=en&sel=[0-9]+(&|$) [NC]
RewriteRule ^(wheels)/$ /$1/index.html [R=301,L,NC]
Btw, lang is not static, it can be en or de.
Try adding this to the .htaccess in the root directory of your website. The querystring parameters are carried through by default.
RewriteEngine on
RewriteBase /
RewriteRule ^wheels/$ /wheels/index.html [R=301,L,NC]
Use this in your ROOT .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{QUERY_STRING} ^lang=en&sel=[0-9]+(&|$) [NC]
RewriteRule ^(wheels)/$ /$1/index.html [R=301,L,NC]
You could also do this by adding an extra RewriteCond in front of the existing RewriteRule
RewriteCond $0 !^wheels/