I want to get access to a IP range with this net feature: 68.241.45.0/20 in .htaccess file and in RewriteCond. Something like this:
RewriteCond %{REMOTE_HOST} !^68.241.45.0/20
but it doesn't seem to work.
With RewriteCond you can only do regular expression or lexicographic comparison. And since 68.241.45.0/20 is the range from 68.241.32.1–68.241.47.254, you could do this:
# regular expression comparison
RewriteCond %{REMOTE_HOST} ^68\.241\.(3[2-9]|4[0-7])\.\d+$
# lexicographic comparison
RewriteCond %{REMOTE_HOST} ^68\.241\.(\d+)\.\d+$
RewriteCond %1 >31
RewriteCond %1 <48
RewriteCond uses regular expression on the second parameter. So you need to escape the dots (.) and use regex for the range.
^68\.241\.[32|47]\.[1|2]?[0-9]+?
Related
I'm having a brain fade and need some help please. I'm using 3 RewriteRules to accomplish something that I think should take just one:
RewriteRule ^([0-9]+)$ /bar/$1.html [R=301,L]
RewriteRule ^([0-9]+)(-*)$ /bar/$1.html [R=301,L]
RewriteRule ^([0-9]+)-([0-9]+)$ /bar/$1.html#$2 [R=301,NE,L]
I need to take the following URLs:
http://foo.com/100
http://foo.com/100-1
http://foo.com/200-
http://foo.com/1999
http://foo.com/1999-99
...and rewrite them like this:
http://foo.com/bar/100.html
http://foo.com/bar/100.html#1
http://foo.com/bar/200.html
http://foo.com/bar/1999.html
http://foo.com/bar/1999.html#99
What I have works but seems like a bit of a hack. is there a way to combine this all in to one rule?
I don't see a way to combine all three rules into a single rule, because the replacement structure is not always the same, with hash sometimes appearing and sometimes not appearing. But you can combine the first two rules:
RewriteRule ^([0-9]+)-?$ /bar/$1.html [R=301,L]
The second rule, which replaces with a hash symbol, can remain as is:
RewriteRule ^([0-9]+)-([0-9]+)$ /bar/$1.html#$2 [R=301,NE,L]
You can combine all 3 rules into one with this trick:
RewriteCond %{REQUEST_URI} ^/(\d+)-?(\d+)?$
RewriteCond %1#%2 ^(\d+)#$ [OR]
RewriteCond %1#%2 ^(\d+)(#\d+)$
RewriteRule ^ /bar/%1.html%2 [R=301,L,NE]
In the first condition, we match regex pattern that starts with a number followed by an optional hyphen and another optional number.
Next two conditions are using [OR] so only one will be true.
For URI /100, first condition will be true and 100 will be captured in %1 but %2 will be empty.
For URI /100-1, second condition will be true and 100 will be captured in %1 but %2 will be #1.
I have a downloadable file with the filename containing a version number, let's say download-3-0.dat, would be the current one.
I want to redirect external references to previous versions of the file to the current one so download-2-5.dat would redirect to download-3-0.dat
This seems a partial solution:
RewriteRule .* - [E=CURRENT:3-0]
RewriteRule ^download(.*).dat$ http://site.com/download-%[ENV:CURRENT].dat [L, R=301]
But the problem is how do you add an exception to the current version so it does not enter a recursion loop (the last rewrite rule would redirect to itsef if the requested file is download-3-0.dat)
You can replace all of your code with this line:
RewriteRule ^download-(?!3-0\.dat$) /download-3-0.dat [L,R=301,NC]
EDIT: Based on your comment below :-
Hmm this turned out to be way more tricky than I anticipated at start.
Try this code:
# set your current version here
SetEnvIf Request_URI "^" CURRENT=3-0
RewriteCond %{ENV:CURRENT}:%{REQUEST_URI} !^([^:]+):/download-\1\.dat$ [NC]
RewriteRule ^download-[^.]+\.dat$ /download-%{ENV:CURRENT}.dat [L,R=301,NC]
PS: Since we cannot use % variables on RHS of RewriteCond as back-reference, I am using special regex back-reference variable \1 in the RewriteCond here.
I think you just need a RewriteCond that confirms it isn't the current version:
RewriteRule .* - [E=CURRENT:3-0]
RewriteCond %{REQUEST_URI} !^download-3-0.dat
RewriteRule ^download(.*).dat$ http://site.com/download-%{ENV:CURRENT}.dat [L, R=301]
You may have to tweak the pattern in the RewriteCond, but this should get you headed in the right direction.
OK, say my blog site myurl.org as many links to a separate domain:
old.myurl.org?oldvar=foo
Only old.myurl.org no longer exists and has been replaced by new.myurl.org.
If the query string vars were the same on new.myurl.org, I believe I could rewrite from .htaccess using:
RewirteCond %{http_host} ^old.myurl.org$ [NC]
RewriteRule ^(.*)$ new.myurl.org [L,R=301,QSA]
The only problem is that I also need to change the query string var from oldvar to newvar and preserve it's data (foo).
There are plenty of examples of how to rewrite query string vars in different ways, but I can't seem to find an example of this scenario.
I need to rewrite:
old.myurl.org?oldvar=foo
To:
new.myurl.org?newvar=foo
Edit
Furthermore, I have several potential query string key values to account for, but not all will always be present.
So I may need:
old.myurl.org?oldvar=foo&oldvar2=bar --> new.myurl.org?newvar=foo&newvar2=bar
or
old.myurl.org?oldvar2=bar --> new.myurl.org?newvar2=bar
This may not even be possible, but in one case I need to strip off part of the query string value. So ?oldvar=foo{term} might need to become ?newvar=foo
Thanks again!
First condition match the domain, second condition match the query string and the rule will match your domain root:
RewriteCond %{HTTP_HOST} ^old\.myurl\.org$ [NC]
RewriteCond %{QUERY_STRING} ^oldvar=(\w+)$
RewriteRule ^$ http://new.myurl.org/?newvar=%1 [R=301,L]
You can place more than one query string as well:
RewriteCond %{HTTP_HOST} ^old\.myurl\.org$ [NC]
RewriteCond %{QUERY_STRING} ^oldvar=(\w+)$ [OR]
RewriteCond %{QUERY_STRING} ^oldvar=(\w+)&oldvar2=(\w+)$
RewriteRule ^$ http://new.myurl.org/?newvar=%1&newvar2=%2 [R=301,L]
RewriteCond %{HTTP_HOST} ^old\.myurl\.org$ [NC]
RewriteCond %{QUERY_STRING} ^oldvar2=(\w+)$
RewriteRule ^$ http://new.myurl.org/?newvar2=%1 [R=301,L]
To use a path like x/y/z as you have mentioned on the comment you change the RewriteRule for example the rule we are currently using, will only redirect from yourdomain.com/?... which is:
RewriteRule ^$ http://new.myurl.org/?newvar2=%1 [R=301,L]
If you want to catch a different path you would do this:
RewriteRule ^x/?$ http://new.myurl.org/?newvar2=%1 [R=301,L]
The above would catch yourdomain.com/x?... and yourdomain.com/x/?...
You can have more than one path as well and use a OR condition like this:
RewriteRule ^(x|x/y|x/y/z)/?$ http://new.myurl.org/?newvar2=%1 [R=301,L]
The above means we want to match x/?... OR x?... OR x/y?... and x/y/?... OR x/y/z?... OR x/y/z/?...
By encapsulating it on the parenthesis and using the | as separator and OR.
The ^ and $ means match from begin to end, so when it contains nothing means match nothing which means root folder domain.com/, when there is content it will match the content for instance domain.com/x or whatever you place into it.
I got these two urls:
/portfolio/stamped_concrete
/p_details.php?id_cat=23&?id=91
I want to make the second url rewrite to:
/portfolio/stamped_concrete/23/91
stamped_concrete is a dynamic url which is why I'm at a loss of how to solve this. Also the two files (portfolio.php and p_details.php) are in the same directory if that matters.
How would I accomplish this?
EDIT:
stamped_concrete is also a variable string that I rewrote before and it works:
RewriteRule ^services/([a-z0-9_-]+)$ /services.php?url=$1 [NC,L,QSA]
so how would I call it within the RewriteRule?
would this be on the right track?
RewriteCond %{QUERY_STRING} url=([a-z0-9_-]+)
RewriteCond %{QUERY_STRING} id_cat=([0-9]+)
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule /p_details.php?.* /portfolio/$1/$2/$3
Try this:
RewriteCond %{QUERY_STRING} id_cat=([0-9]+)
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule /p_details.php?.* /portfolio/stamped_concrete/$1/$2
Might need to tweak it a bit -- not sure if the RewriteRule part is correct (sorry).
But, the important part is QUERY_STRING, see the apache docs: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Also, from http://wiki.apache.org/httpd/RewriteQueryString
I always mix up the backreferences, so try it out, it's eiher dollar signs or percent signs (i really thought it was dollar signs...)
(QUOTE)
Making the Query String Part of the Path
Take a URL of the form http://example.com/path?var=val and transform it into http://example.com/path/var/val. Note that this particular example will work only for a single var=val pair containing only letters, numbers, and the underscore character.
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^/path /path/%1/%2?
(END QUOTE)
So you could probably just say "RewriteRule ^/p_details.php /portfolio/%1/%2/%3"
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.