htaccess RewriteRule with hard-coded special characters - .htaccess

There are a lot of similar questions, but none of them seem to deal with hard-coded strings in the destination. I merely want to redirect any requests for a sub-domain to a Google Group, such as follows:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} gg.domain.com$
RewriteRule ^(.*)$ https://groups.google.com/forum/#!forum/myforum
I believe the problem is that the # character is being interpreted as a comment and thus the line is ignored. I have looked through the documentation for rewrite flags, and the only options I saw which might be relevant were [B] and [NE], neither of which seem to help, as I think they only work on string transformations.

The # character in your url should not be treated as a comment (it isn't for me). If it is treated as such though, you can escape it to let it loose it's special meaning. Escaping is the act of puting a \ before a character. Besides that, you require the [NE] flag to prevent the # from being ascii encoded.
RewriteCond %{HTTP_HOST} gg.domain.com$
RewriteRule ^(.*)$ https://groups.google.com/forum/\#!forum/myforum [NE]

Related

Allow %0A in url RewriteRule with htaccess

I've got a rewriterule in my .htaccess which allows me to add unlimited parameters separated by /'s.
RewriteRule ^(.*)/?$ index.php?params=$1 [L,NC]
This works properly untill I send an urlencoded string to it (using cURL) with an encoded \n in it (%0A).
So server/param1/param2/param3text works, but server/param1/param2/param3text1%0Aparam3text2 doesn't.
I found one Q on Stack Overflow mentioning a similar problem:
How can I apply an htaccess rewrite rule to a URL containing a linefeed character (%0A)?
But I can't/don't know how to implement [\r\n] in my (.*).
Any help?
Ok, so first, I had to add a check to make sure that the file didn't exist (the two RewriteCond's take care of that). Then I had to create a pattern that matched any character, or a \r or a \n that was matched one or more times(+). The zero or more times operator (*) didn't return the results properly.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((.|\r|\n)+)/? index.php?params=$1 [L,NC]
Just an FYI here: A common hacking method called Whitespace filtering uses %0A
Filtering can be bypassed on the space character by using alternative
whitespace characters to the space character (%20). Most SQL engines
consider a line return (%0a in a *NIX environment, %0a%0d in a Windows
environment), tab characters, or the + character as valid whitespace:
You must utilize %{THE_REQUEST} variable to grab actual path from original Apache web server request.
Try this code:
RewriteCond %{QUERY_STRING} !^params=.+ [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+[^/]+/([^\s]+) [NC]
RewriteRule ^ index.php?params=%1 [L,QSA]
Then inside index.php check $_SERVER["QUERY_STRING"] for the full unadulterated path with %0A in it.

Is my query string redirect right?

I want to redirect mypage.com/store/index.php?product_id=62&thisvar=doesntmatter to mypage.com/store#page-anchor. My current redirect looks like this, but doesn't seem to be doing anything. Am I doing something wrong?
RewriteCond %{QUERY_STRING} ^([^&]&)*product_id=62(&|$)
RewriteRule ^/store/index\.php$ /store#page-anchor [R=301,L]
Edit: I should note that I have the following lines before my rewrite rule:
RewriteEngine On
RewriteBase /
The ([^&]&)* bit doesn't look quite right. It matches strings like "a&b&c&" (a pair of a non-& character and a &, repeated any number of times), and that's probably not what you want. I guess you wanted to write ([^&]*&)*, but I'd suggest (^|&)product_id=62(&|$) (for safety, readability and elegance).
Oh and, if it's in your .htaccess (and not your httpd.conf), the pattern in the RewriteRule should be a relative URI, i.e. it shouldn't start with a /.

allow Special Characters in URL

I want to allow special charecters in URL
my htaccess code is
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^blog/?$ SocialNetwork/blog.php [L]
RewriteRule ^blog/(\d+)/?$ SocialNetwork/blog.php?id=$1 [L]
RewriteRule ^blog.php/(\d+)/?$ SocialNetwork/blog.php?id=$1 [L]
RewriteRule ^blog.php/(\d+)/([A-Za-z0-9-_[]]+)/?$ SocialNetwork/blog.php?id=$1&title=$2 [L]
</IfModule>
it works for
blog/1
blog/1/hii
blog/1yooo_title
but does not work for
blog/1/[hii-this is [] title
In your last rewrite rule, try escaping the square brackets. Also if you need to match spaces you need to include \s in there:
^blog.php/(\d+)/([A-Za-z0-9-_\[\]\s]+)/?$
Or perhaps consider the simpler which accepts anything in the title:
^blog.php/(\d+)/(.+)/?$
Another point is that you should not have spaces in your URLs. They should be escaped to "+" or %20. So depending on this your regex would change, except the last one I proposed should work.
I'm pretty sure that you can't use spaces in a URL. Ever.
only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL.
http://www.ietf.org/rfc/rfc1738.txt

.htaccess questions

Say I have the following .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_COOKIE} name=value [NC]
RewriteRule ^image01.gif$ http://www.domain.tld/images/partner/image01.gif [NC,QSA]
RewriteCond %{HTTP_COOKIE} name=value [NC]
RewriteRule ^image02.gif$ http://www.domain.tld/images/partner/image02.gif [NC,QSA]
What do NC and QSA mean?
Also, instead of repeating the same RewriteCond twice is there to use it just once and have it apply to both RewriteRules?
Finally, if the above .htaccess is located at http://www.domain.tld/images/ why doesn't a RewriteRule like this work?:
RewriteRule ^image02.gif$ /images/partner/image02.gif [NC,QSA]
Or maybe this?:
RewriteRule ^image02.gif$ partner/image02.gif [NC,QSA]
The square bracket options are documented in the RewriteRule manual page:
'nocase|NC' (no case):
This makes the Pattern case-insensitive, ignoring difference
between 'A-Z' and 'a-z' when Pattern is matched against the current URL.
'qsappend|QSA' (query string append):
This flag forces the rewrite engine to append a query string part
of the substitution string to the
existing string, instead of replacing
it. Use this when you want to add more
data to the query string via a rewrite
rule.
As far as I know, the RewriteCond directives affect the RewriteRule they precede. If you were setting rules in the main confing file you could write the common directives in a file and include it several times but that's not an option in .htaccess files, sorry.
Your directive works for me, although you probably mean this:
RewriteRule ^image02\.gif$ /images/partner/image02.gif [NC,QSA]
How are you testing it exactly?
NC is for No Case, meaning it can be upper or lower case and it will take you to the same page.
QSA is for query string append. Not really sure on this one, however a quick search http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html sheds a bit more light on this one.

Do you have to escape a forward slash when using mod_rewrite?

With regards to the forward slash "/" when giving a regex to RewriteRule or RewriteCond, or anything else related to .htaccess in particular, is there a need to escape the forward slash?
Here is an example of what I am trying to achieve
RewriteEngine on
RewriteOptions inherit
RewriteBase /uk-m-directory/
RewriteRule ^(region|region\/|regions\/)$ regions [R=301,L]
RewriteRule ^(county|county\/|counties\/)$ counties [R=301,L]
RewriteRule ^(city|city\/|cities\/)$ cities [R=301,L]
The above works fine, and it continues to work fine when I remove the backslashes as shown below
RewriteEngine on
RewriteOptions inherit
RewriteBase /uk-m-directory/
RewriteRule ^(region|region/|regions/)$ regions [R=301,L]
RewriteRule ^(county|county/|counties/)$ counties [R=301,L]
RewriteRule ^(city|city/|cities/)$ cities [R=301,L]
Which one is the correct way? Are they both wrong?
Is there any special reason the forward slash should be escaped, or shouldn't?
My guess is that the forward slash does not need to be escaped because it isn't a special character, as far as I know. But I just want to be sure.
In case you're wondering the point of this code, it redirects city, county, and region (with or without a forward slash) to their plural equivalents. Furthermore if the plural has a forward slash it removes the forward slash.
No, you do not have to escape slashes. Forward slashes don't have any special meaning in regular expressions.
The one common character that has bitten me in the past is ? in query strings. That one you do have to escape.

Resources