Fixing a RewriteRule that adds two extra slashes - regex

I'd like to redirect web requests from example.org to www.example.org. I think I have the right rule, but it is adding two extra forward slashes:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.org [NC]
RewriteRule ^(.*)$ http://www.example.org/$1 [R=permanent,L]
The above would take me from example.org to www.example.org//
Is there a way to strip those trailing slashes? I tried the following variant, which strips those slashes but causes a redirect loop:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.org [NC]
RewriteRule ^(.*)$ http://www.example.org/$1 [R=permanent,L]
RewriteRule ^(.*)/$ /$1 [L,R=301]
How might I solve this?

Try adding RewriteBase / right after RewriteEngine on.
If that doesn't do it, change your rule to RewriteRule ^/(.*)$ http://www.example.org/$1 [R=301,L] so you don't capture the initial /.

Related

How i can stop processing of htaccess?

I've got two directives in my htaccess.
RewriteRule ^(it|en|de)/(.*) $2?lng=$1 [L,QSA]
That means: for all url with start with it, en, de set url variables "lng" to $lang
Now, i want that all pages that didn't start with a language code to be redirected to /it:
I try this:
RewriteCond %{REQUEST_URI} !^/(it|en|de)/{0,1}(.*)
RewriteRule ^(.*)$ /it/$1 [R=301,L]
but when i call:
http://HOST/my-page
on url, i get, with a "ERR_TOO_MANY_REDIRECTS":
http://HOST/it/my-page?lng=it&lng=it&{many-others-lng=it}
the word "/it" is added correctly at the beginning of the url, but also infinity "lng=it"
I use "L" flag: this shoud stop processing the rule set.
Any hint?
EDIT:
Add my full .htaccess
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteRule ^(it|en|de)/(.*) $2?lng=$1 [L,QSA]
RewriteRule ^catalogue-men$ views/main/pages/index.cfm?gen=1 [QSA,L]
RewriteCond %{REQUEST_URI} !^/it/
RewriteRule ^(.*)$ /it/$1 [R=301,L]
Just remember that mod_rewrite runs in loop until there is no rule that fires. You are getting redirect loop because first rule is removing /it/ from start of URI and 301 rule is adding it in front.
Reordering your rules
Using THE_REQUEST instead of REQUEST_URI
Add /catalogue-men in your skip condition
This .htaccess should work for you:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} !\s/+(?:it|catalogue-men)/ [NC]
RewriteRule ^(.*)$ /it/$1 [R=301,L,NE]
RewriteRule ^(it|en|de)/(.*) $2?lng=$1 [L,QSA]
RewriteRule ^catalogue-men$ views/main/pages/index.cfm?gen=1 [QSA,NC,L]

Why does my htaccess not rewrite properly

I am having a problem with my htaccess file, and can't figure out if it's my configuration, or something like server cache which is messing with my URLs.
My file is the following
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^website.com$
RewriteCond %{HTTPS} off
RewriteRule (.*) https://website.com/$1 [R=301,L]
RewriteRule ^([a-z]+)\/?$ $1.php [NC]
The first rule is to move www to non-www, it works.
The second rule is to move http to https, it also works.
the third rule is to make URL /anyFile call anyFile.php, but keep a lovely URL.
The problem is that it calls anyFile.html, not PHP, and if I remove said HTML file then I get 404.
Yes it works typing anyFile.php in the URL, but I would like to not have .php in the URL.
If it is not obvious enough, it is supposed to work for any file name, not just a single one.
Any and all help is much appreciated.
Replace your .htaccess rules with this code:
Options -MultiViews
RewriteEngine On
RewriteBase /
# single rule for http->https and www removal
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://website.com/$1 [R=301,L]
# hide .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([a-z]+)/?$ $1.php [NC,L]
I'm recently learn how to do htaccess too, so I'm still a novice but I would write
RewriteRule ^([a-z]+)\/?$ helloWorld.php [NC]
to redirect it to the helloWorld.php file.
The $1 is a get parameter.

Apache Rewrite Issue. Unable to figure out query string

So I have been struggling on finding the rule to match this rewrite. I am working on a client website and it is a nightmare with the number of duplicate title tags. I have managed to resolve most of them by enforcing forward slash, redirect non www. to the www. version and disallow crawling of https version of the website.
The issue I am having at the moment. I have over 1000 URLs that are duplicate content, each product has two different URLs with the exact same content. An example is:
http://www.example.co.uk/product/widget1/
http://www.example.co.uk/widget1/
http://www.example.co.uk/product/widget2/
http://www.example.co.uk/widget2/
Now the following URLs have the same content:
http://www.example.co.uk/product/widget1/
http://www.example.co.uk/widget1/
I want to redirect any URL that contains "/product/" to the URL version without "/product/" in the URL if that makes sense. I honestly don't know where to start and would really appreciate the help.
Thanks in advance
EDIT: The recommended rule:
RewriteEngine On
RewriteRule ^/product/(.*)$ /$1 [R=301]
does not work. It may be conflicting. These are the other rules:
RewriteEngine On
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ /$1? [R=301,L]
RewriteRule ^/product/(.*)$ /$1 [R=301]
RewriteCond %{HTTP_HOST} ^example\.co [NC]
RewriteRule (.*) http://www.example.co.uk/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
I dont know if there are any conflicts here. Please help
Have your full .htaccess like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.co [NC]
RewriteRule (.*) http://www.example.co.uk/$1? [L,R=301]
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ /$1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?[^/])$ /$1/ [L,R=301]
RewriteRule ^product/([^/]+)/?$ /$1/ [R=301,L]
Assuming the URLs always start with product, this should work:
RewriteEngine On
RewriteRule ^/product/(.*)$ /$1 [R=301]
It'll need to go in your main site conf or .htaccess

How to handle both /url and /url/ redirects with htaccess?

I am rewriting all URLs and shorthand some of them:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(([^\.]+))\.pl [NC]
RewriteRule ^(.*)$ http://%1.pl%{REQUEST_URI} [R=301,L]
RewriteRule ^test$ test-page.php [R=301,L]
so that
example.pl/test redirects to example.pl/test-page.php
but
example.pl/test/ redirects to example.pl/
while it should:
example.pl/test/ redirects to example.pl/test-page.php
How to handle both /url and /url/ redirects?
I assume that it has to something with trailing slash.
You need to make trailing slash optional using /?$ regex. Try these rules:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.pl [NC]
RewriteRule ^ http://%1.pl%{REQUEST_URI} [R=301,L]
RewriteRule ^test/?$ test-page.php [NC,R=301,L]
Make sure to test this in a new browser to avoid 301 caching issues.
Try this:
RewriteEngine On
RewriteRule ^(.*)/$ http://%{HTTP_HOST}/$1
RewriteRule ^test$ test-page.php [R=301,L]
Tested here.

mod_rewrite: remove www and trailing slash

I already have a rule set up to remove www from my urls and redirect them...
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*) http://'%'1$1 [R=301,L]
*note-I had to put quotes around the percent sign to post this message, the actual rule does not contain them.
I now want to also strip off any trailing /
How would I do this?
Add an extra rule:
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [L,R=301]