htaccess redirect append query string? - .htaccess

I have an htaccess redirect that needs to forward the query string to the new URL, but it's getting dropped after the redirect. Can someone tell me what's wrong?
RewriteRule ^services/agents.*$ https://services.example.com/agents/ [R=301,L,QSA]

The same rule is working fine on my server. The problem should be something else.
I added the same rule on my server and I get the following redirect
http://mysite.com/services/agents/foo?foo=bar => https://services.mysite.com/agents/?foo=bar
Please note that you don't need to add the QSA flag since the target doesn't include any query string.
This article might contain some useful information to help you dealing with Htaccess and Query String.

In general there is no need to explicitly append the query or use the QSA flag if you don’t specify a query for the substitution. But as you said your rule doesn’t work, try this:
RewriteRule ^services/agents.*$ https://services.example.com/agents/?%{QUERY_STRING} [R=301,L]

Related

.htaccess rewrite rule for affiliate links

I've many links of this type:
https://example.com/?redirectTo=G04BIQ8LGG&redirect_prodid=xyz-G04BIQ8LGG
I need to redirect to amazon affiliate link:
https://www.amazon.com/dp/G04BIQ8LGG?tag=mytag-21&linkCode=ogi&th=1&psc=1
The only part to take from old url is the product code (ex.G04BIQ8LGG)
Someone can help me with .htaccess rule and regex?
Thanks!
unfortunately no, I'm not very good with regex.
The regex is very similar as in the linked question. However, the required mod_rewrite directives themselves are much simpler in this case.
For example:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^redirectTo=([^&]+)
RewriteRule ^$ https://www.amazon.com/dp/%1?tag=mytag-21&linkCode=ogi&th=1&psc=1 [NE,R=302,L]
I've anchored the redirectTo URL parameter to the start of the query string, since that is how it appears in your example. In the linked question, the URL parameter can appear anywhere in the query string since that would seem to have been a requirement in that question.
Since the URL parameter value is used in the URL-path of the redirected URL, the NE (noescape) flag is required to prevent a %-encoded URL param value being doubly encoded in the resulting redirect. (Although this is not an issue if this URL param value is never %-encoded - it doesn't necessarily look as if it would be.)

.htaccess, virtual directories, and semi-complex URLs

I'm basically just trying to have a master syntax for predictable URLs. Simple URL is no problem
RewriteEngine on
# RewriteRule ^friendlyUrl/content/?$ /index.php?app=main&module=content
Which to my understanding looks for the url structure and allows 1 or 0 trailing "/"'s
But some parts of the website have a /urlPrefix/ to access, eg. mysite.com/membersArea/
and /membersArea/ will be apart of every query there. I'm having trouble accomodating for trailing ?s and &s in URLs like these.
RewriteRule ^secureUrl/\?(.*)$ /index.php?app=admin&$1
This is my attempt to handle everything from mysite.com/secureUrl/ to mysite.com/secureUrl/?var1=foo&var2=bar and after many server errors and a search, I find myself here.
This is the most complex line I have and between you and me, I couldn't tell you exactly what's happening other than it looks for /friendlyUrl/10DIGITKEY/(possible task)/?possiblevars=foo&var2=bar
RewriteRule ^friendlyUrl/([a-zA-Z0-9]{10})/?([a-z]*)/?\??(.*)$ /index.php?app=main&module=web&id=$1&$2&$3
Htaccess has always been my weakest subject, and as a webmaster I pay the price constantly, any help would be appreciated.
Need to input the same request to the PHP file (plus ANY query with or without ? or &) whether its just /friendlyUrl/ or /friendyUrl/?var=1, /friendlyUrl/&var=1, /friendlyUrl/var=1
You're looking to keep the query string of your request URI to remain as is, or to be included in the rewritten URL after the rewrite process is done.
For this purpose, you use the QSA flag in your RewriteRule directive. So, to rewrite /friendlyUrl/10DIGITKEY/(possible task)/?possiblevars=foo&var2=bar, you'd have:
RewriteRule ^friendlyUrl/([a-z\d]{10})/([^/]*)/?$ /index.php?app=main&module=web&id=$1&task=$2 [QSA]
Notice the QSA flag at the end. Also, keep in mind that I'm passing the second match (the possible task of your URL) as another variable (named task). This variable will be empty if nothing was found.
QSA|qsappend
When the replacement URI contains a query string, the default behavior
of RewriteRule is to discard the existing query string, and replace it
with the newly generated one. Using the [QSA] flag causes the
query strings to be combined.

Getting Query String item after htaccess redirect

I have a re-written URL that will look like one of the following:
http://www.example.com/anyfolder/some-more-here/
http://www.example.com/anyfolder/some-more-here/?ref=referralcode
Here is the rule I was playing with:
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/?$ /page.asp?folder=$1&url=$2&refcode=$3 [NC,L]
My question is that since (http://www.example.com/anyfolder/some-more-here/?ref=) is a rewritten URL that has an additional query string item on it after the rewrite is done, how do I get that "ref" item in my code? I can easily get "anyfolder" and "some-more-here" by doing a simple request, but I am confused on the "ref" value part. I think the QSA flag might be needed, just having trouble bringing it all together.
I am using Classic ASP on this site, but the solution probably applies to PHP as well.
Any help would be great. thank you in advance!
Dennis
ok as per your comment you do need QSA flag. Try your rule as:
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/?$ /page.asp?folder=$1&url=$2&refcode=$3 [NC,L,QSA]
This will preserve ?ref=... in the rewritten URI.
QSA (Query String Append) flag preserves existing query parameters while adding a new one.

Rewrite rule in htaccess to keep parameters

I've written the following rewrite rule which works fine when no parameters (no page numbers, no products per page, no sort order etc)
RewriteRule ^(?!bench/).*cat_2.html(\.[a-z]{3,4})?(.*) "http\:\/\/www\.mysite\.co\.uk\/bench\/cat_2\.html\?mode\=allBrands" [R=301,L]
This makes sure the URL is optimized on Googles results. So
http://www.mysite.co.uk/bench-clothing/cat_2.html?mode=allBrands#
gets changed to
http://www.mysite.co.uk/bench/cat_2.html?mode=allBrands
mode=allBrands will always be set.
So if i click on a link to go to
http://www.mysite.co.uk/bench-clothing/cat_2.html?mode=allBrands&ppp=64&sort=desc&page=2
it gets redirected to
http://www.mysite.co.uk/bench/cat_2.html?mode=allBrands
which is the first page.
Any help would be great.
change [R=301,L] to [R=301,L,QSA]
More info : https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa
You need to add a QSA flag to your rewrite rule, so that the brackets look like this:
[R=301,L,QSA]
This tells apache to append any existing query string to the new query string in the target (mode=allBrands).

URL Rewriting based on form input

I'm creating a frontpage for my website with a single form and input text, Google-style. It's working fine, however, I want to generate a pretty URL based on the input. Let's say, my input is called "id", and using the GET method of form, and the action defined to "/go/", on submission, the URL will be:
site.com/go/?id=whateverIType
and I want to change it to
site.com/go/whateverIType
I was thinking on Mod Rewrite, but if the user put something in the URL, like:
site.com/go/?dontwant=this&id=whateverIType&somemore=trash
I want to ignore the other variables but "id", and rewrite the rule.
What's the better way of get this done? Thanks in advance!
PS: I'm using CodeIgniter, maybe there's something I can use for it as well. I already have a controller for "go".
I'm not familiar with CodeIgniter, but you can try the following RewriteRule
RewriteEngine on
RewriteCond %{REQUEST_URI} ^\/go\/
RewriteCond %{QUERY_STRING} id=([^&]*)
RewriteRule (.*) /go/%1? [L,R]
The %1 references the regex group from the previous RewriteCond, and the trailing ? will strip the querystring from the redirected URL.
Hope this helps.
Mod_rewrite supports conditions and rules with RegEx, so you could have a rule that matched the ?id=XXXX, that would extract it from the URL (keeping the other parameters), and rewrote the URL accordingly.
However... I don't think you want to do this, because if you rewrite the URL to be /go/Some+Search+Query, you won't be able to pick it up with say, PHP, without parsing the URL out manually.
It's really tough to have custom, SEO-friendly URLs with user input, but it is technically possible. You're better off leaving in the ?id=XXX part, and instead, using mod_rewrite in the opposite approach... take all URLs that match the pattern /go/My+Search+Terms and translate that back into something like ?id=My+Search+Terms, that way you'll be able to easily parse out the value using the URL's GET parameters. This isn't an uncommon practice - Google actually still uses URL parameters for user input (example URL: http://www.google.com/search?q=test).
Just keep in mind that mod_rewrite rewrites the URL before anything else (even PHP), so anything you do to the URL you need to handle. Think of mod_rewrite as a regular expression-based, global "Find and Replace" for URLs, every time a page is called on the server. For example, if you remove the query string, you need to make sure your website/application/whatever accounts for that.
In application/config/routes.php
$route['go/(:any)'] = "go/index/$1";
Where go is your controller and index is the index action.
http://codeigniter.com/user_guide/general/routing.html
You can use something like this in your .htaccess if you aren't already:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Resources