Add GET parameter in htaccess - regex

I am looking for a way to add a rule to the htaccess that will automatically add a GET parameter to a request coming in on a subdomain (the reason why is a long story and seems irrelevant).
So sub.example.com should redirect to sub.example.com?parameter=test
The problem I'm having is the circular reference. There must be a way to avoid this?

You can try this code:
RewriteEngine On
RewriteCond %{QUERY_STRING} !(^|&)parameter=test(&|$) [NC]
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteRule ^ %{REQUEST_URI}?parameter=test [L,R=301,QSA]

Related

Redirect specific urls from old domain to new domain and retain parameters

Here is a sample old URL:
https://www.olddomain.com/index.php?option=com_k2&view=itemlist&task=calendar&month=8&year=1904&catid=39&Itemid=339
Which needs to be redirected to:
https://www.newdomain.com/index.php?option=com_k2&view=itemlist&task=calendar&month=8&year=1904&catid=39&Itemid=339
Since there are countless month,year,catid,and itemid I wanted to catch them all with one rule.
I was trying to create a htaccess redirect for any URL that has "/index.php?option=com_k2&view=itemlist&task=calendar".
This was my best attempt:
RewriteEngine on
RewriteRule ^/index.php?option=com_k2&view=itemlist&task=calendar(.*)$ https://www.newdomain.com/index.php?option=com_k2&view=itemlist&task=calendar$1 [R=301,L]
You may be able to use this redirect rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^option=com_k2&view=itemlist&task=calendar [NC]
RewriteRule ^ https://newdomain.com%{REQUEST_URI} [R=301,L,NE]
Make sure this your topmost rule and you refresh your browser cache before testing this rule.

Rewriting a subdomain to root/subdomain

I currently have the following location: localhost/dir1/ex.html
I'm trying to achieve a situation where dir1.localhost/ex.html maps to that first url. The following .htaccess rule works:
RewriteCond %{HTTP_HOST} =dir1.localhost
RewriteRule ^(.*)$ http://localhost/dir1/$1 [L]
However, this is not what I am looking for as it quite obviously redirects. I want to keep the url the same (dir1.localhost).
How would one go about doing this? Many thanks in advance.
If you want your subdomain to point to the folder internally ,Remove the hostname from your rule's destination :
RewriteCond %{HTTP_HOST} =dir1.localhost
RewriteCond %{REQUEST_URI} !^/dir1
RewriteRule ^(.*)$ /dir1/$1 [L]`

RewriteCond when REQUEST_URI do not match htaccess apache2

I have a multilingual wordpress website and want to redirect website of given region to given language,
xyz.de --> xyz.de/de/
xyz.co.uk --> xyz.co.uk/en/
direct access to xyz.de/de and xyz.co.uk/en are working properly. So there is no problem on wordpress side.
Now, I am trying to change the htaccess file of xyz.de and xyz.co.uk so that they redirect the website.
Considering xyz.co.uk
I want to add a RewriteCond such that whenever there is no /en trailing after xyz.co.uk it will automatically add /en.
For example xyz.co.uk/<trailing address> results in xyz.co.uk/en/<trailing address>
So far I have the following code, which somehow doesn't seem to work,
RewriteCond %{REQUEST_URI} !^/en
RewriteRule ^(.*)$ http://xyz.co.uk/en/$1 [L]
The negation of /en is not working! I have also tried
RewriteCond %{REQUEST_URI} !/en
RewriteRule ^(.*)$ http://xyz.co.uk/en/$1 [L]
Could someone tell me where I am going wrong? seems like I have gone wrong in writing RegEx and suggest if there is better way to achieve the same, that does not affect the SEO across different domains.
Use THE_REQUEST variable instead of REQUEST_URI:
RewriteCond %{HTTP_HOST} \.co\.uk$ [NC]
RewriteCond %{THE_REQUEST} !/en/ [NC]
RewriteRule ^ /en%{REQUEST_URI} [L,R=302,NE]
Make sure to keep this rule as your very first rule in .htaccess.
Change it to R=301 once you've tested.

Rewrite URL's with variable query string placement with .htaccess

I'm trying to rewrite a few different URL's so they redirect to different places, however they aren't static URL's.
I need to redirect:
/index.php?app=core&section=register
to for example:
htttp://www.mysite.com/register.php
However, what I want to do is just make sure that /index.php?app=core&section=register starts with /index.php? but there may be more parameters included and also want to make sure it's still redirected if they are in a different position, so basically it just needs to match it if app=core and section=register exists in the URL.
Lastly, if I wanted to add a second one such as..
/index.php?app=core&section=login to htttp://www.mysite.com/login.php how would I add a second one?
Try this rule:
RewriteCond %{QUERY_STRING} (?:^|&)app=core(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)section=(register|login)(?:&|$) [NC]
RewriteRule ^index\.php$ /%1.php? [NC,L,R=302]
As per comments:
RewriteCond %{QUERY_STRING} (?:^|&)app=core(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)section=(somepath)(?:&|$) [NC]
RewriteRule ^index\.php$ /mypath.php? [NC,L,R=302]

How to redirect a URL with a specific parameter to a URL without one?

So basically, I am trying to remove a parameter from all URLs within my site.
For example:
http://www.mydomain.com.au/thispage.html?view=form
http://www.mydomain.com.au?view=form
http://www.mydomain.com.au/this-is-a-page.html?view=form
I require all the pages above to go to their version without this parameter, such as:
http://www.mydomain.com.au/thispage.html
http://www.mydomain.com.au
http://www.mydomain.com.au/this-is-a-page.html
The redirect must only occur when view=form, and not when view is equal to anything else.
Very new to regex and apache, so I am not to sure if this is even possible.
So far I have just been using the following code to point them to a 404, although this has been causing a few problems within webmaster tools:
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com\.au [NC]
RewriteCond %{QUERY_STRING} (^|&)view=
RewriteRule ^ - [L,R=404]
Use the following htaccess code
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)view=form(&|$) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI}? [R=301,L]
In case you want to preserve other query parameters (like ?foo1=bar1&view=form&foo2=bar2) use
RewriteCond %{QUERY_STRING} ^(?:(.*)&)?view=form(?:(&.*))?$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI}?$1$2 [R=301,L]