Remove ?query=string from URL with htaccess - regex

I used to have a query string for handling mobile pages. Now the site is responsive it doesn't need it and I need to do 301 redirects let google know.
so
http://www.example.com/aaa/bbb?fromMobile=true
should become
http://www.example.com/aaa/bbb
and
http://www.example.com/aaa?fromMobile=true
should become
http://www.example.com/bbb
and
http://www.example.com?fromMobile=true
should become
http://www.example.com
etc
I have:
RewriteCond %{QUERY_STRING} ^(?)fromMobile=true$ [NC]
RewriteRule (.*) /$1?%1&%2 [R=301,L]
But this redirects:
http://www.example.com?fromMobile=true
to
http://www.example.com?
How can I get rid of the trailing question mark? I've tried a bunch of things without joy.

You can use:
RewriteCond %{QUERY_STRING} ^fromMobile=true$ [NC]
RewriteRule (.*) /$1? [R=301,L]
This is assuming there is no other query parameter besides fromMobile=true.
If there is a chance of other query parameters also then use:
RewriteCond %{QUERY_STRING} ^(.+?&)?fromMobile=true(?:&(.*))?$ [NC]
RewriteRule (.*) /$1?%1%2 [R=301,L]

Related

Handle one query parameter specifically

I need to handle one query parameter specifically.
The url can look like the following:
https://example.com/?app=egov&map=1337
https://example.com/?app=egov&map=1337&showoptions=true
https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorld
The redirect should point to
https://example.com/contextName/resources/apps/egov/index.html?map=1337
https://example.com/contextName/resources/apps/egov/index.html?map=1337&showoptions=true
https://example.com/contextName/resources/apps/egov/index.html?map=1337&anotherparam=helloWorld
As you can see, the app-Parameter needs to be used as an URL-part; the others need to be used like traditional query-params.
I've figured out, how to implement the first part, where https://example.com/?app=egov points to https://example.com/contextName/resources/apps/egov/index.html using the following
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^app\=(.+)
RewriteRule ^/$ "/contextName/resources/apps/%1/index.html" [R,L,QSA]
but I'm struggeling how to realize the correct handling of the other params.
All these combination of query string can be handled by a single rule loke this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^app=([\w-]+)(?:&(.*))?$ [NC]
RewriteRule ^$ contextName/resources/apps/%1/index.html?%2 [L]
RewriteCond matches app query parameter and captures it in %1 while rest of the query string after & is captured in %2.
With your shown samples, please try following htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
####Apache documentation link https://httpd.apache.org/docs/trunk/rewrite/remapping.html for more info.
##Internal rewrite rule for URL https://example.com/?app=egov&map=1337
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2 [L]
##Internal rewrite rule for URL https://example.com/?app=egov&map=1337&showoptions=true
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=(.*)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2&showoptions=%3 [L]
##Internal rewrite rule for https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorl
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=([^&]*)&anotherparam=(.*)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2&anotherparam=%4 [L]
All of your URLs uri part is NULL, so rules are written based on that.

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]

Redirect to www and eliminate fb_xd_fragment

I've been trying all all day to combine these two rewrites into just one -or get both to work consecutively-. They work perfectly well on their own, but not together.
I need to redirect in apache all access to a website to a "www" subdomain, and at the same time I need to eliminate the "fb_xd_fragment" added by facebook; while keeping intact any other query strings.
This are the two separate pieces:
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1
and
RewriteCond %{QUERY_STRING} ^(.*)fb_xd_fragment(=)?$
RewriteRule (.*) %{REQUEST_URI}?%1 [R=301,L]
Try adding a L after the first rule:
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^(.*)fb_xd_fragment(=)?$
RewriteRule (.*) %{REQUEST_URI}?%1 [R=301,L]

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

Regex for url-encoded string in .htaccess

I have to addresses for one site (different langs) and I need a redirect with saving query string. So I need to catch this
http://www.example1.kereell.com/wp-login.php?action=logout&redirect_to=http%253A%252F%252Fwww.example2.kereell.com%252Fafiliados-de-la-zona%252F&_wpnonce=66909cdca0
and redirect to
http://www.example2.kereell.com/wp-login.php?action=logout&redirect_to=http%253A%252F%252Fwww.example2.kereell.com%252Fafiliados-de-la-zona%252F&_wpnonce=66909cdca0
in .htaccess file it should be something like this
RewriteCond %{HTTP_HOST} ^.*example1\.kereell\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/wp-login.php?action=logout&redirect_to=http%3A%2F%2Fwww.example2.kereell.com%2Fafiliados-de-la-zona%2F&_wpnonce=66909cdca0$ [NC]
RewriteRule ^(.*)$ http://www.example2.kereell.com/$1 [R=301,L]
I tried a lot of everything (escaping "%", regex classes) but didn't get it to work because of urlencoded characters. If I replace all urlencoded symbols - it works..
So the solution was really easy as it suppose to be:
RewriteCond %{HTTP_HOST} ^.*example1\.kereell\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/wp-login.php$ [NC]
RewriteCond %{QUERY_STRING} ^action=logout&redirect_to=http[\%A-Z0-9]*www.example2.kereell.com(.*)$ [NC]
RewriteRule ^(.*)$ http://www.example2.kereell.com/$1 [R=301,L]
Thanks for Qtax that paid my attention to QUERY_STRING!