I have the following RewriteCond in htaccess:
#RewriteCond %{HTTP_HOST} ^xyzdomain.com$
Now I would like to use a placeholder for the string "domain" in xyzdomain.com so that the condition matches with any domain like xyzhome.com, xyztravel.com, xyzshop.com, xyz*.com, etc.
Which placeholder can I use for the string?
It's just a regex. You can do something like:
RewriteCond %{HTTP_HOST} ^xyz(.+)\.com$
The (.+) will match "any character one or more times".
Also note the \ escaping the . in .com. . is a special wildcard character with regular expressions so you have to escape it when you want to explicitly match ..
regular-expressions.info is a great place to learn more about how they work.
Related
I need to create a redirect rule to lowercase.
All URLs starting with /catalog/ must be lowercase. example:
/catalog/Foo => /catalog/foo
/catalog/Foo/fOO2 => /catalog/foo/foo2
Can you help me?
Assuming you are on Apache 2.4 then you can use the tolower() function in an Apache expression in a mod_rewrite RewriteCond directive to perform the uppercase to lowercase conversion on these URLs.
For example:
RewriteEngine On
RewriteCond expr "tolower(%{REQUEST_URI}) =~ /(.*)/"
RewriteRule ^catalog/.*[A-Z] %1 [R=302,L]
The RewriteRule pattern checks for a URL-path that starts catalog/ followed by an uppercase character. The RewriteCond directive then converts the URL-path to lowercase, which is captured in the regex and the corresponding backreference %1 is used in the RewriteRule substitution string as the target for the redirect. %1 contains the lowercase'd URL-path.
Here I got such url
http://minecraftstorage/add
And this rule
RewriteRule ^add/? add.php [L]
But that rule affects not only add string which is in URL, but also additionals string which is in the path to Bootstrap css.
So how can I make it to work only for add and not for additionals?
Change to RewriteRule ^add/?$ add.php [L]
$ denotes the end of a regex (regex = regular expression) test string.
I have this Rewrite rule:
RewriteRule ^([a-zA-Z0-9-]+)/([0-9]+)/?$ /home.php?id=$1&slug=$2 [L,QSA]
which currently rewrites things like home.php?id=blog&slug=123 to be /blog/123
how can i change it so the 123 is text and numbers rather than just numbers?
I have this in my htaccess file that is redirecting:
home.php?id=services to /services
i want to also be able to do:
home.php?id=blog&slug=slug-goes-here to be /blog/slug-goes-here
Replace the [0-9]+ part with [\w-]+. This will match the character 0-9, a-z, A-Z, _ (the underscore) and - (the hyphen):
RewriteRule ^([a-zA-Z0-9-]+)/([\w-]+)/?$ /home.php?id=$1&slug=$2 [L,QSA]
Also, you might want to read something about the basics of regular expressions: http://www.regular-expressions.info/
I want to check if a URL contains the sting "-EN.htm", if so apply the rewrite.
That should be done with ^-EN.htm as follows, but the rule is not working:
RewriteCond %{REQUEST_URI} ^/(.*?)/([-_0-9a-zA-Z./=]*)^-EN.htm
RewriteRule ^(.*)$ /indexEN.php?folder=%1&follow=%2 [L]
What am I doing wrong?
Thank you for every help,
Scott
Your regular expression doesn't look right. You can also lose the condition and just move the pattern to the rewrite rule instead. Something along the lines of
RewriteRule ^/?(.*?)/([-_0-9a-zA-Z./=]*)^-EN.htm /indexEN.php?folder=$1&follow=$2 [L]
You need to make the leading slash optional (in htaccess this is stripped off) and instead of using % backreferences, use the $ ones.
Now on to your pattern, it's not valid. The ^ matches the beginning of the string (the URI), so if you have two of them and you're not trying to literally match the ^ character (which you'd need to escape), then the expression will never match anything. Without any examples of URLs that you're having to deal with, I assume you probably just want to ditch the second ^:
RewriteRule ^/?(.*?)/([-_0-9a-zA-Z./=]*)-EN.htm /indexEN.php?folder=$1&follow=$2 [L]
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]+?