Apache Rewrite Issue. Unable to figure out query string - regex

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

Related

Write `.htaccess` regex to redirect URL to previous category/sub-directories

I have a site at www.test.com. The site has many nested directories and categories such as www.test.com/cat/1/321/.
I want to use a Regex to remove any url ending in /321/ or /321 to the previous category.
Example: Redirect www.test.com/cat/1/321/ to www.test.com/cat/1/
Needed for a Wordpress site using .htaccess. Thanks!
You can write your conditions and rules for exam in this code we have to conditions for /321/ and /321
RewriteCond %{REQUEST_URI} /321/$ [NC]
RewriteRule ^(.*)/321/$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} /321$ [NC]
RewriteRule ^(.*)/321$ /$1 [R=301,L]
Simple enough
RewriteEngine On
RewriteRule ^(.*/)321/?$ $1 [R,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.

How to properly write a RewriteCond for https and subdomains

Thus far I've gotten this far with my RewriteConditions but it's from hacking and slashing from internet examples. They seems to behave a little funny at times, like showing the directory in the url, so I assume I'm not doing it right.
The desired behaviour: I have multiple subdomains all pointing to the same root. Based on the subdomain value I want to redirect to a sub-folder in the root. I also want to force HTTPS. I have multiple subdomains, but I'll only show two for the sake of brevity. All the other conditions are virtually identical.
Here's my .htaccess code:`
RewriteEngine On
RewriteCond %{HTTP_HOST} purchase.mydomain.com [NC]
RewriteCond %{REQUEST_URI} !purchase/
RewriteRule (.*) https://purchase.mydomain.com/purchase/$1 [L]
RewriteCond %{HTTP_HOST} booking.mydomain.com [NC]
RewriteCond %{REQUEST_URI} !booking/
RewriteRule (.*) https://booking.mydomain.com/booking/$1 [L]
`
Thank you,
Mike
Change your rules like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(purchase|booking)\.mydomain\.com$
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} =purchase.mydomain.com
RewriteRule ^((?!purchase/).*)$ /purchase/$1 [L]
RewriteCond %{HTTP_HOST} =booking.mydomain.com
RewriteRule ^((?!booking/).*)$ /booking/$1 [L]
http:// or https:// in target will redirect instead of rewriting.

htaccess redirect rule from www.domain.com/?page=news to www.domain.com/news

i need to redirect the URL as
www.domain.com/?page=news --> www.domain.com/news
Here my htaccess file:
Options -Multiviews
RewriteEngine On
RewriteBase /
# Force search engines to use www.domain.com
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteRule ^(.*) http://www.domain.com/$1 [R=301,L]
# Specify search friendly URLs
RewriteRule ^http://www\.domain\.com/news/$ /http://www.domain.com/?page=news [L]
Please suggest me the exact rule to use in .htaccess file.
Thanks in advance.
Your 2nd rule is not correct as you can't match domain name in RewriteRule pattern. That pattern only matches REQUEST_URI without domain name and query string.
Your 2nd rule should be like this:
RewriteCond %{THE_REQUEST} \s/+\?page=([^&\s]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Specify search friendly URLs
RewriteCond %{QUERY_STRING} !(^|&)page=[^&]+
RewriteRule ^([^/]+)/?$ /?page=$1 [L,NC,QSA]
Reference: Apache mod_rewrite Introduction
Sorry, I do not know a thing about this, but I have seen a good tutorial posted by someone I am following on facebook:
http://www.9lessons.info/2013/11/htaccess-file-tutorial-and-tips.html
Hope this helps.

regex in my RewriteRule is OK, but .htaccess does not work

I'm having a lot of trouble getting my .htaccess ReWrite to work on my apache web server. I've read several tutorials and tested my regex matching with Grep.
Here is the code:
RewriteRule \?action=viewArticle&articleId=([0-9]*)&categoryId=([0-9])$ essays/$1 [R=301,L]
here is a url I'm trying to match:
http://mysite.com/?action=viewArticle&articleId=15&categoryId=1
and change to
http://mysite.com/essays/15
UPDATE: Solution! with a very excellent tutorial from Jon. It was very important that I put <base href="/"> in my header file to get the css to work correctly.
Final rewrite looked like this:
RewriteCond %{THE_REQUEST} /?action=viewArticle&articleId=([0-9]*)&categoryId=([0-9])
RewriteRule ^$ /essays/%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^essays/([0-9]+) /?action=viewArticle&articleId=$1&categoryId=([0-9]) [L]
You can't match hosts or query strings inside a RewriteRule, you need to match against the %{HTTP_HOST} and %{QUERY_STRING} variables in a RewriteCond directive:
RewriteCond %{HTTP_HOST} mysite\.com$ [NC]
RewriteCond %{QUERY_STRING} ^action=viewArticle&articleId=([0-9]*)&categoryId=([0-9])$
RewriteRule ^$ /essays/%1? [L,R=301]
This redirects the browser (changing the URL in the address bar) when someone goes to http://mysite.com/?action=viewArticle&articleId=15&categoryId=1 to http://mysite.com/essays/15
You don't need the %{HTTP_HOST} condition if your htaccess file only serves a single host.