mod_rewrite: query string gets lost on rewrite - .htaccess

I have limited .htaccess knowledge and requires some help. I need to redirect all page request to www.newdomain.com except for www.olddomain/page.json but the query string get dropped when it redirect. how can i preserve it? thankyou very much!
Current code:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/page.json
RewriteRule (.*) http://newdomain.com/$1 [R=301,L]
Edit: Just to make it clear, i only need to stay on page.json on the old domain, lets say user request for www.olddomain.com/page1.json?session=gVgr30ExUlM
i need to redirect to
www.newdomain.com/page1.json?session=gVgr30ExUlM
BUT when it is www.olddomain.com/page.json?=LKJHGF i need it to stay on that old domain and wont redirect is it possible?

Use the [QSA] flag ("query string append")

You need to add [QSA] and optionally [NE] which should give you:
RewriteRule (.*) http://newdomain.com/$1 [QSA,NE,R=301,L]

Related

.htaccess help! redirecting URL with query string to root

I'm new to htaccess so please bear with me..
I'm trying to redirect traffic to URLs with query string to the root of my site as these URLs are no longer used.
Here is an example:
I would like traffic to this URL
https://www.DomainName.com/subdir1/subdir2/cond_e.asp?obark=110246
to go to http://www.DomainName.com
I do not need to keep the URL or anything just redirect to my homepage.
I need to do this for query string value from obark=110246 all the way up to obark=120000.
I've basically played around with the below to get at least one URL to redirect properly but never could get it to work at all.
RewriteCond %{QUERY_STRING} ^oPark=110246$
RewriteRule ^subdir1/subdir2/cond_e(.*) ^$? [NE, R=301, L]
What am I doing wrong? please help!
Try with below rule in subdir2,
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/cond_e.asp
RewriteCond %{QUERY_STRING} ^obark=[1]{1}[1-2]{1}[0]{1}[02-9]{1}[094-5]{1}[06-9]{1}
RewriteRule ^ http://www.DomainName.com? [R=301,L]

.htaccess 301 redirect that excludes a subpage

My rule looks like this:
RewriteRule ^page-parent.*$ http://www.domain.com/new-page/?%{QUERY_STRING} [R=301,L]
My issue is that there is a page /page-parent/thanks that I don't want to be redirect.
I am also passing the query string along so that any ?gclid= string will go with.
I can't for the life of me figure out how to exclude a single sub page or all sub pages, which would work too.
Any help is appreciated.
You can check with a RewriteCond if the requested page is the page you want to exclude.
RewriteCond %{REQUEST_URI} !^/page-parent/thanks
RewriteRule ^page-parent.*$ http://www.domain.com/new-page/ [R=301,L,QSA]
Make sure that you clean the browser cache when retrying since 301-redirects get cached.
Note: You don't need to append the query string manually.
You can use mod_rewrite to create a condition:
RewriteCond %{REQUEST_URI} !^/page-parent/thanks
RewriteRule ^page-parent.*$ http://www.domain.com/new-page/ [R=301,L]
Note that you can leave out the ?%{QUERY_STRING} from your target, because query strings are appended to the target by default unless you've added new params via a ?.

.htaccess redirect to a directory and retain $_GET variables?

I want to redirect from /orders/whatever/?n=4
to:
/orders/?shopURL=whatever&n=4
I managed to redirect to the correct directory but I'm losing the passed variables.
This is my current rule:
RewriteRule ^([\w\d]+)$ /orders/?shopURL=$1 [L]
If you want to enforce the existence of a query string, use:
RewriteCond %{QUERY_STRING} (\bn=\d+\b)
RewriteRule ^orders/([\w\d]+)/?$ /orders/?shopURL=$1&%1 [L]
Otherwise, you can use the following without a RewriteCond:
RewriteRule ^orders/([\w\d]+)/?$ /orders/?shopURL=$1 [L,QSA]
The QSA flag to RewriteRule will retain existing query string fields.
Your RewriteRule seems very generic, as though it would redirect almost every page.
I'm not that great with Regular Expressions so I'll leave those to you, or someone else. But you need something at the end of the rewrite rule to catch parameters, then append it at the end of the new URL.
RewriteRule ^([\w\d]+)/(somethinghere)$ /orders/?shopURL=$1&$2 [L]

.htaccess redirecting secure

Haven't found what i'm looking for so far.
I want to redirect all my site visitors to the secure version of the URL they type in, so i've put this in my .htaccess-file.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L]
So far so good, but now I want that visitors who type in
www.mywebsite.com/directory/param1/etcetera/ or
www.mywebsite.com/directory/file.php?param1=value1
are being redirected to
https://www.mywebsite.com/directory/param1/etcetera/ or
https://www.mywebsite.com/directory/file.php?param1=value1
as well, cause that isn't happening.
Can some guru help me out? :) thnx
Your first case, www.mywebsite.com/directory/param1/etcetera/, should be redirected to its https variant by your RewriteRule. I can't see why that wouldn't work.
To cover the second case, in order to have Apache include the query string, you need to add the QSA flag, so:
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L,QSA]

How to remove part of a URL using .htaccess

I have a url like this.
/domains/details.php (NOTE: domains is dynamic and can be anything)
How do I remove the domains part from the URL using .htaccess so the actual lookup is:
/details.php
OR it'll be cool if I can get domains into the URL.
/details.php?page=domains
Thanks!
Scott
RewriteEngine on
RewriteBase /
RewriteRule ^([^/]+)/details.php$ /details.php?page=$1 [R=301]
Leave off the [R=301] if you want an internal redirect rather than an actual HTTP redirect.
To preserve existing query parameters you can change the rule to this:
RewriteRule ^([^/]+)/details.php(.*)$ /details.php?page=$1&%{QUERY_STRING} [R=301]
Please try to use the following rules to deal with your last request:
RewriteRule ^(?!domains/.*)([^/]+)/details.php$ domains/details.php?page=$1 [R=301,QSA]
RewriteRule ^domains/details.php$ details.php [NC,L]

Resources