.htaccess redirect only if GET parameter exist - apache

I have the following line in my .htaccess file:
Redirect 301 /folder1 https://www.example.com/folder2/file.php
This will redirect everything from /folder1 to https://www.example.com/folder2/file.php.
I need a condition to only allow this redirection if the URL contains a mykey= GET parameter, else ignore this redirection command.
How can I do that?

You cannot do this using Redirect directive that does basic URI matching.
You will need to use mod_rewrite based rules for this like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)mykey= [NC]
RewriteRule ^folder1(/|$) /folder2/file.php [R=301,L,NC]
Make sure to clear your cache before testing.
References:
Apache mod_rewrite Introduction

I finally found the a working solution:
RewriteCond %{REQUEST_URI} ^/folder1/
RewriteCond %{QUERY_STRING} mykey=
RewriteRule ^folder1\/$ /folder2/file.php$1 [R=301,L]

Related

htaccess rule for redirecting when parameters are present

RewriteEngine On
RewriteRule ^$ /index.phtml [R,L]
RewriteRule ^index.phtml.+$ /index.php$1 [R,L]
I have 2 urls like https://www.example.com/index.phtml and https://www.example.com/index.php which both point to very different sites. I want to redirect to index.php in case there are any parameters found after index.phtml.
For example, https://www.example.com/index.phtml?a=b should go to https://www.example.com/index.php?a=b (second rule). I am trying the above rules but the second one isn't matching (the first rule works fine). Thanks for any help/suggestions
Please ask if you need any more details.
You may use this code in site root .htaccess:
RewriteEngine On
# if there is a query string then redirect
# /index.phtml to /index.php
RewriteCond %{QUERY_STRING} .
RewriteRule ^(?:index\.phtml)?$ /index.php [L,NC]
# redirect landing page to /index.phtml
RewriteRule ^$ /index.phtml [R=302,L]
Note that QUERY_STRING get automatically appended to target URL so /index.phtml?a=b will be redirected to /index.php?a=b.
Make sure to clear your browser cache before testing this change.
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your redirect rules.
With your shown samples, please try following .htaccess rules file. Please make sure that your htaccess file and index.php are present in same folder.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##For without query string rules.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.phtml/?$ index.php [R=301,NC,L]
##For query string rules.
RewriteCond %{THE_REQUEST} \s.index\.phtml\?([^=]*=\S+)\s [NC]
RewriteRule ^ index\.php?%1 [R=301,L]

Htaccess rewrite domain to a new domain with the appending path based on the parameter given

To clarify, I want to redirect:
domain.page.co.uk/path/?lang=de
to
de.page.co.uk/path/
where path will change according to the page I am on.
I want to keep the path of my own domain without the appending parameter at the end but based on my parameter, change the new domain accordingly. So that I can add more rewrite rules for different languages, i.e. lang=fr.
I have tried a few ways but none of them were successful. I am not familiar with htaccess rewrite rule syntaxes. Here are what I have tried based on what I could find online:
RewriteRule ^/([^/d]+).page.co.uk/?$ ^?lang=$1 [L,QSA]
RewriteCond %{REQUEST_URI} ^(.*)/?lang=de$
RewriteRule ^(.*) http://de.page.co.uk/%1 [R=302,NC]
RewriteRule ^de.(.*)/?$ ^(.*)\?lang=de [NC,L]
You'll want something like:
RewriteCond %{HTTP_HOST} ^domain.page.co.uk
RewriteCond %{QUERY_STRING} lang=(de|fr|it) [NC]
RewriteRule .* http://%1.page.co.uk%{REQUEST_URI}? [R=301,L]
For an explanation see: URL Aliasing, Redirection, Rewriting and Reverse Proxying using Apache HTTPD

.htaccess Rewrite Rules, friendly URLs + redirect

I'm using .htaccess to take URLs that look like:
/index.php?page=2
and rewrite them to, for example:
/contact-us
I want to do two things:
When loading /contact-us, show page /index.php?page=2 but keep the friendly URL. I know this looks something like:
RewriteRule ^contact-us$ "index\.php\?page\=2" [L]
Which does work ok. But now I also want people who navigate to /index.php?page=2 to end up on /contact-us - how do I achieve this as a 301 redirect in combination with the friendly URL rewrite?
Not sure why you are using quotes and escapes in target portion of your rewrite rule. The target portion is not a regular expression. That can simply look like:
RewriteRule ^contact-us$ /index.php?page=2 [L]
To redirect the index.php?page=2 you will need to do the following. This rule MUST be before the rule above or you will get in rewrite loop.
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^page=2$
RewriteRule .* /contact-us [R=301,L]
Here you are setting two rewrite conditions: that the page is /index.php and that the query string is page=2 (and only page=2). The R=301 flag will force an external rewrite to /contact-us and also send and HTTP 301 header.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /index\.php\?page=2[&\s] [NC]
RewriteRule ^ contact-us? [R=302,L,NE]
RewriteRule ^contact-us/?$ index.php?page=2 [L,QSA,NC]

.htaccess Rewrite Rules for subdomain

I use codeigniter as my main install on the main domain. I have created a subdomain and a folder called live e.g. live.domain.com maps to public/live . However in public I use codeigniter.
I now have the dynamic codeigniter url:
http://domain.com/api/
which I want to map to my subdomain:
https://live.domain.com
So going to:
https://live.domain.com/api/functioname
would be using the script:
http://domain.com/api/apifunctioname
and possibly:
http://domain.com/api/apifunctioname/parameter1/parameter
Everything is on the same server so no redirects are needed.
Anyone have any ideas on which rewrite rules to use?
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^live\.domain\.com [NC]
RewriteRule (.+)$ "http://domain.com/api/$1" [L]
The above works great as a rewrite but redirects to http://domain.com/api/functionname instead I want it to route; so that when going to:
https://live.domain.com/api/functioname
It stays at that url but uses the script of
http://domain.com/api/functionname
Thank you very much,
Ice
How about something like the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^live\.domain\.com$ [NC]
RewriteRule (.+)$ "https://domain.com/api/$1" [L,P]
Just add index.php, see below
RewriteEngine On
RewriteCond %{HTTP_HOST} ^live\.domain\.com$ [NC]
RewriteRule (.+)$ "https://domain.com/index.php/api/$1" [L,P]

Apache Redirect problem in .htaccess

I am having problems getring a simple redirect statement to take effect on my Godaddy account. I have the following statements in my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mydomain.net$ [NC]
RewriteRule ^(.*)$ http://mydomain.net/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain.net$ [NC]
RewriteRule ^/lists/$ / [R=301]
RewriteCond %{HTTP_HOST} ^mydomain.net$ [NC]
RewriteRule ^/blog/$ http://myotherdomain.net/ [R=301]
The 1st redirect ALWAYS work. The 2nd and 3rd ones however, NEVER work. I just get a 404 from the server. The Apache logs do not reveal any useful infomation - just a 404.
Any ideas, anybody?
Your help will be greatly appreciated.
Thanks
Per-directory Rewrites
When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the pattern matching and automatically added after the substitution has been done.
– http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule
So just leave the leading slash out of the pattern.
For simple redirects like that, better use the simple RedirectMatch directives:
RedirectMatch 301 ^/lists/$ http://mydomain.net/
RedirectMatch 301 ^/blog/$ http://myotherdomain.net/
If you insist on using rewriting make sure you add the L flag to your rules.
Apache mod_rewrite Flags says :
You will almost always want to use [R] in conjunction with [L] (that is, use [R,L]) because on its own, the [R] flag prepends http://thishost[:thisport] to the URI, but then passes this on to the next rule in the ruleset, which can often result in 'Invalid URI in request' warnings.
Simply remove the slashs at the beginning. It also might be useful to make the slashs at the end optional.
RewriteCond %{HTTP_HOST} ^mydomain.net$ [NC]
RewriteRule ^lists/{0,1}$ / [R=301]
RewriteCond %{HTTP_HOST} ^mydomain.net$ [NC]
RewriteRule ^blog/{0,1}$ http://myotherdomain.net/ [R=301]
Put the first one last. Once it encounters a redirect match, it runs it and ignores the rest.