Welcome to WebmasterWorld Guest from 54.209.227.199

Forum Moderators: Ocean10000 & phranque

htaccess code needed for a parameter string

     
5:33 pm on Nov 12, 2018 (gmt 0)

Junior Member

Top Contributors Of The Month

joined:Dec 20, 2017
posts: 85
votes: 0


I have
https://www.example.com/123.html?language=en
https://www.example.com/123.html
for all the products in my site

Can you give me an htaccess code to send all of the parameter strings
with "?language-english" to the basic url without parameters?
Thanks
Art
7:02 pm on Nov 12, 2018 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11681
votes: 205


using the RewriteRule directive:
Modifying the Query String
By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.

from https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

[edited by: phranque at 11:08 pm (utc) on Nov 12, 2018]

7:33 pm on Nov 12, 2018 (gmt 0)

Junior Member

Top Contributors Of The Month

joined:Dec 20, 2017
posts: 85
votes: 0


i am no programmer so would this be substitution string
and if so, what do i do with it?

?https://www.example.com/123.html?

Sorry but i dont know what a [qsa] flag is.
9:42 pm on Nov 12, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15636
votes: 795


#1 Have you ever used Rewrites for any purpose? What rules have you currently got in your htaccess involving either mod_alias (rule beginning in Redirect) or mod_rewrite (rulesets beginning in RewriteRule)? We need to start by figuring out your level of knowledge or, ahem, lack thereof.

#2 Do the URLs that you want to change ever have other parameters that you need to keep? Or is it purely a matter of dumping the whole query string (the part after the ? question mark)?

This isn't a Write My Code For Me forum. The idea is to help you learn how to do it yourself. If you want a cut-and-paste solution, you'll need to go over to That Other Forum ... but be prepared for snotty replies telling you the question has previously been asked and answered. You just can't win :)
12:15 am on Nov 13, 2018 (gmt 0)

Junior Member

Top Contributors Of The Month

joined:Dec 20, 2017
posts: 85
votes: 0


I know these 4 sets of rules for fixing non www's to www, http to https, and index.htm to root domain
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteEngine On
RewriteRule ^index\.htm$ / [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteEngine On
RewriteRule ^index\.html$ / [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteEngine On
RewriteRule ^index\.php$ / [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

__________________________________________________________
Here are some of the rewrite rules on the site just now :
Your Question 1:
--------------------
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

#RewriteCond %{ENV:HTTPS} !on [NC]
#RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ https://www.example.com/$1 [R=301,L]

RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)-p-(.*).html$ product_info.php?products_id=$2&%{QUERY_STRING}
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)-c-(.*).html$ index.php?cPath=$2&%{QUERY_STRING}
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$


# IOSC mobile site
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)-mp-(.*).html$ mobile/product_info.php?products_id=$2&%{QUERY_STRING}
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)-mc-(.*).html$ mobile/catalog_mb.php?cPath=$2&%{QUERY_STRING}


Your question 2
---------------------
I want to dump the query part, after the string

Thanks
Art

[edited by: phranque at 5:00 am (utc) on Nov 13, 2018]
[edit reason] unlinked urls [/edit]

1:43 am on Nov 13, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15636
votes: 795


Eeuw. Existing rules are all in the wrong order (in particular, the domain-name-canonicalization rule should be the last external redirect, and the index.php redirect should be the second-to-last), some of the Rules and Conditions seem to be garbled, a lot of the Rules look redundant, and ...

Oh well. We'll talk about that another time. The main thing I wanted to find out is whether you've already got RewriteRules (answer: yes) and whether you’ve got mod_alias redirects (answer: apparently no), which is as it should be.

Are we dealing with a recurring query string that could show up at the end of any URL? Are the URLs really in the form 123.html (one or nore numerals)? I assume you didn't mean literally "123.html" with only one possible URLpath. Do they always end in html? The idea is to constrain the rule as narrowly as possible, so the server can take a quick look, say “nope, nothing to see here” and scurry along to the next ruleset. So we are somewhere on the spectrum of

RewriteCond %{QUERY_STRING} language=en
RewriteRule ^(\d+\.html) https://www.example.com/$1? [R=301,L]
or
RewriteCond %{QUERY_STRING} language
RewriteRule ^(\w+\.html) https://www.example.com/$1? [R=301,L]
or
RewriteCond %{QUERY_STRING} .
RewriteRule ^([^.]+\.html) https://www.example.com/$1? [R=301,L]
et cetera, where you pick one from Column A and another from Column B:

For the body of the RewriteRule, pick the narrowest pattern that will match all possible URLs.

For the RewriteCond, conversely, pick the broadest pattern that will match.
%{QUERY_STRING} ^language=en$
means the query string can consist only of this exact parameter-and-value set
%{QUERY_STRING} language=en
without ^ $ anchors means that "language=en" can occur anywhere in the query string, but the other stuff can be thrown away too
%{QUERY_STRING} language
means that you don't care what value the "language" parameter has, because you're getting rid of everything. And finally
%{QUERY_STRING} .
means that you don't care what the query string says at all: if it's present, get rid of it.

The target of the Rewrite--note the final question mark--
https://www.example.com/$1?
means "keep the same URLpath, but get rid of any and all query strings”.

In Apache 2.4 you can use the flag [QSD] (Query String Delete) instead of putting a ? at the end of the target, though I've yet to learn why you'd want to. Maybe it's just a personal-coding-style-preference thing.

[edited by: phranque at 7:38 pm (utc) on Nov 13, 2018]
[edit reason] typo: \1 vs $1 [/edit]

5:16 am on Nov 13, 2018 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11681
votes: 205


if you are getting requests for https://www.example.com/index.php?language=en you will also need to add the trailing question mark to the substitiution string on the corresponding RewriteRule to avoid chained redirects.

i.e. https://www.example.com/index.php?language=en -> https://www.example.com/index.php -> https://www.example.com/

you'll want something like this:
RewriteRule ^(.*)/index.php$ https://www.example.com/$1/? [R=301,L]

RewriteCond %{QUERY_STRING} language=en
RewriteRule ^(\w+\.html) https://www.example.com/$1? [R=301,L]

RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]


the order of rulesets above should be followed but the specifics should be adjusted appropriately for your application.

[edited by: phranque at 7:42 pm (utc) on Nov 13, 2018]

6:02 pm on Nov 13, 2018 (gmt 0)

Junior Member

Top Contributors Of The Month

joined:Dec 20, 2017
posts: 85
votes: 0


Lucy 24. Thanks for the very detailed reply.
Here are my answers to some of your questions

Answering your questions:

1. Yes we are dealing with a recurring query string that could
show up at the end of any url

2.Urls to be redirected, are of the form
example.com/alphanumericsandnumbers.html?language=en

3.There are several url paths after example.com, but they all end in .html
I just want to redirect any url that has the path
?language=en
to
example.com/alphanumericsandnumbers.html

You gave 3 suggestions
---------------------------------------------------------------------------------------
RewriteCond %{QUERY_STRING} language=en
RewriteRule ^(\d+\.html) https://www.example.com/\1? [R=301,L]

or
RewriteCond %{QUERY_STRING} language
RewriteRule ^(\w+\.html) https://www.example.com/\1? [R=301,L]

or
RewriteCond %{QUERY_STRING} .
RewriteRule ^([^.]+\.html) https://www.example.com/\1? [R=301,L]

I think it is the first one:
RewriteCond %{QUERY_STRING} language=en
RewriteRule ^(\d+\.html) https://www.example.com/\1? [R=301,L]
Am i right?
If i pick these 2 line, is the Rewrite rule correct?

The point about the $1? I don't understand because that is not in your suggested lines of code.
7:20 pm on Nov 13, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15636
votes: 795


First things first: Arturo, I had a horrendous brain fart when typing, so anywhere I said \1 it should really be $1. The \1 form is my text editor's syntax; I have no idea how it sneaked into a RewriteRule. I have asked moderators to fix it for posterity. (Unfortunately this means that future readers of this thread will say “wtf is he talking about? There's no \1 anywhere!” but on balance it's the better change.)

When I said "one from column A and one from column B" I meant that there's no connection between the three possible RewriteRules and the three possible RewriteConds. In each case, pick the one that fits. If
^(\d+\.html)
fits all incoming requests, then use that one. And then, separately, pick the RewriteCond that fits your situation: exactly "language=en", or query containing the element "language=en", or query with the "language" parameter, or even any-query-at-all. If the object is to get rid of all possible query strings--which is certainly possible if those are genuine html pages--then I'd go with
RewriteCond %{QUERY_STRING} .
RewriteRule ^(\d+\.html) https://www.example.com/$1? [R=301,L]
where that lone . in the Condition means "any content at all". It means the server only has to check for the existence of any character, instead of matching all eleven bytes of "language=en". And then the ? in the target means "get rid of it all".
12:00 pm on Nov 14, 2018 (gmt 0)

Junior Member

Top Contributors Of The Month

joined:Dec 20, 2017
posts: 85
votes: 0


Ok, thanks for the update.;
I am 50% of the way there.

This will be my new line 1. (It is exactly just for queries with language=en)
RewriteCond %{QUERY_STRING} language=en

-----------------------------------------------------------------------------------
My last hurdle is the RewriteRule.
I have 3 rewrite rule suggestions and I don't know what they do :
\d or \w or ^[^]

I want
https://www.example.com/123.html?language=en
to redirect to
https://www.example.com/123.html

Of your 3 original RewriteRule suggestions, which one should i use for the rewrite rule ?
6:19 pm on Nov 14, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15636
votes: 795


I thought you'd already settled that one. The pattern
^\d+\.html
matches any URL that (1) is located at the root (no subdirectories) and (2) consists entirely of numerals and (3) ends in html

Some people like to say [0-9] instead of \d. This is entirely a matter of personal preference; they mean exactly the same thing.
7:07 pm on Nov 14, 2018 (gmt 0)

Junior Member

Top Contributors Of The Month

joined:Dec 20, 2017
posts: 85
votes: 0


(2) its actualy alphacharacters and numerals.

So does that mean i should use
RewriteRule ^(\w+\.html) https://www.example.com/\1? [R=301,L]
or
RewriteRule ^([^.]+\.html) https://www.example.com/\1? [R=301,L]

I don't know what the operators \w or ^([^.] actually do.
thanks for bearing with me
7:48 pm on Nov 14, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15636
votes: 795


its actualy alphacharacters and numerals.
Well, now you say so. Yes, in that case use \w instead of \d.

\w means a “word character”, i.e. alphanumerics and also _ (lowline) but not - (hyphen). Like \d, this locution is recognized by all RegEx engines everywhere, so it's good to internalize it.


If your brain is tired, you can stop reading here. What follows is supplementary information.

[^.] means “anything except .” (literal period). In htaccess contexts it tends to be a last resort, as it means “capture everything up to the extension” (assuming you don't have filenames containing . literal periods, which is perfectly legal but often inconvenient). Most often you use this if you've got long messy URLs where any precise pattern like ^(([\w-]+/)+[\w-]+) would be more trouble than it's worth.

Grouping brackets [blahblah] or [^blahblah] have some special rules, notably that . means a literal period instead of “any one character”, and non-final hyphen means a character range, like [0-9] for all numerals.

[0-9] == [0123456789] == \d
== (if you really, really want to annoy your RegEx engine) [^\D]

The form [^blahblah] generically means “anything except blahblah” where blahblah is one or more characters. That is, not the string “blahblah”--for that you'd need a lookahead or lookbehind--but the individual letters b,l,a,h.
12:10 am on Nov 15, 2018 (gmt 0)

Junior Member

Top Contributors Of The Month

joined:Dec 20, 2017
posts: 85
votes: 0


Thank you very much for the explanations
Arturo
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members