I have redirection rule in place on nginx config like,
location ~ ^/abc/(.*) {
rewrite ^www.abc.com/web/primary/abc.html/$1;
}
but I need to exclude few directories of my redirection location for example,
location ~ ^/abc/ae
location ~ ^/abc/be
Those ae and be should not be redirected to above URL. can someone help on this?
Although achievable using location blocks, but you will need to make the blocks functional which could be complex for dynamic content.
The simplest approach would be a negative lookahead assertion in the regular expression.
In your example, the location block is not really necessary and your rewrite directive seems to be missing some white space. But something like this may work for you:
rewrite ^/abc/(?!ae|be)(.*)$ /foo/$1 last;
The (?! sequence begins a regular expression (ae|be) that must not be present at that point. See this document for more.
Related
I am looking to add multiple language support to my website. Is it possible to use the .htaccess file to change something like:
example.com/dir/?lang=en to example.com/en/dir/
example.com/main/?lang=de to example.com/de/main/
example.com/main/page.php?lang=de to example.com/de/main/page.php
Where this works with any possible directories - so if for instance later on I made a new directory, I wouldn't need to add to this. In the above example, I want the latter to be what the user types in/is in the address bar, and the start to be how it is used internally.
Is it possible to use the .htaccess file to change something like:
example.com/dir/?lang=en to example.com/en/dir/
Yes, except that you don't change the URL to example.com/en/dir/ in .htaccess. You change the URL to example.com/en/dir/ in your internal links in your application, before you change anything in .htaccess. This is the canonical URL and is "what the user types in/is in the address bar" - as you say.
You then use .htaccess to internally rewrite the request from example.com/en/dir/, back into the URL your application understands, ie. example.com/dir/?lang=en (or rather example.com/dir/index.php?lang=en - see below). This is entirely hidden from the user. The user only ever sees example.com/en/dir/ - even when they look at the HTML source.
So, we need to rewrite /<lang>/<url-path>/ to /<url-path>/?lang=<lang>. Where <lang> is assumed to be a 2 character lowercase language code. If you are offering only a small selection of languages then this should be explicitly stated to avoid conflicts. We can also handle any additional query string on the original request (if this is requried). eg. /<lang>/<url-path>/?<query-string> to /<url-path>/?lang=<lang>&<query-string>.
A slight complication here is that a URL of the form /dir/?lang=en is not strictly a valid endpoint and requires further rewriting. I expect you are relying on mod_dir to issue an internal subrequest for the DirectoryIndex, eg. index.php? So, really, this should be rewritten directly to /dir/index.php?lang=en - or whatever the DirectoryIndex document is defined as.
For example, in your root .htaccess file:
RewriteEngine On
# Rewrite "/<lang>/<directory>/" to `/<directory>/index.php?lang=<lang>"
RewriteCond %{DOCUMENT_ROOT}/$2/index.php -f
RewriteRule ^([a-z]{2})/(.*?)/?$ $2/index.php?lang=$1 [L]
# Rewrite "/<lang>/<directory>/<file>" to `/<directory>/<file>?lang=<lang>"
RewriteCond %{DOCUMENT_ROOT}/$2 -f
RewriteRule ^([a-z]{2})/(.+) $2?lang=$1 [L]
If you have just two languages (as in your example), or a small subset of known languages then change the ([a-z]{2}) subpattern to use alternation and explicitly identify each language code, eg. (en|de|ab|cd).
This does assume you don't have physical directories in the document root that consist of 2 lowercase letters (or match the specific language codes).
Only URLs where the destination directory (that contains index.php) or file exists are rewritten.
This will also rewrite requests for the document root (not explicitly stated in your examples). eg. example.com/en/ (trailing slash required here) is rewritten to /index.php?lang=en.
The regex could be made slightly more efficient if requests for directories always contain a trailing slash. In the above I've assumed the trailing slash is optional, although this does potentially create a duplicate content issue unless you resolve this in some other way (eg. rel="canonical" link element). So, in the code above, both example.com/en/dir/ (trailing slash) and example.com/en/dir (no trailing slash) are both accessible and both return the same resource, ie. /dir/index.php?lang=en.
I am having problem to rewrite a url that may or may not include a secondary pattern.
This is a rewrite mod for a login system.
I want to rewrite
"contoso.com/register/1/"
and
"contoso.com/register/2/{key}"
However, I found that if I have
^register/([1-2]+).*
I am not going to get the key for step 2 of the registration.
And if I use
^register/([1-2]+)/([_0-9a-zA-Z-]+).*
for my mask. It is not going to rewrite "contoso.com/register/1/" .
Will it be possible to rewrite in just one rule? Or I must have two separate rules for this scenario?
It will be good to help with the IIS presentation. But if you know the method on .htaccess. I am also able to convert myself. Many thanks!
I have done some research on Regex and found that a /* mask will make the rule work while it excludes "/" being detected.
The mask I am having now is
^register/([1-2]+)/*([_0-9a-zA-Z-]+)
Which will take step 1 as {R:1} and key after slash as {R:2}
I have a requirement to make the following paths work.
Depending on what the url consists of, they are mapped to go to different java classes.
/books/
/books/science/
/books/science/fiction/
/books/science/fiction/kids/
So, I have given the rewrite rules in my configuration file as:
^/books$
^/books/(.*)$
^/books/(.*)/(.*)$
^/books/(.*)/(.*)/(.*)$
but the moment I give a url something like this
http://localhost/books/science/fiction/kids/12345
instead of getting captured by the fourth rewrite rule, it is captured by the second one which is not what I want.
Can someone please tell me how to achieve this? Thanks in advance
^/books$ /webapp/wcs/stores/servlet/ABCController?resultsFor=allCategories [PT,QSA]
^/books/(.*)$ /webapp/wcs/stores/servlet/XYZController?make=$1&resultsFor=category [PT,QSA]
^/books/(.*)/(.*)$ /webapp/wcs/stores/servlet/ABCDController?format=$1-$2&resultsFor=subCategory [PT,QSA]
^/books/(.*)/(.*)/(.*)$ /webapp/wcs/stores/servlet/ASDFController?resultsFor=product [PT,QSA]
instead of getting captured by the fourth rewrite rule, it is captured by the second one
That’s because the dot matches any character, so slashes as well.
Replacing it by a character class allowing anything but a slash (and demanding at least one character out of that class, so + instead of *) should fix that: ([^/]+)
Another way would be to reverse the order of your rules … You should always try and write them in order from most to least specific anyway.
I have recently found out that, if there are multiple URLs (eligible for rewriting) in one line of a web page, the IIS / ARR would only rewrite the first match of that line, and ignore the rest. So I'd like to ask two questions:
Is this the default behavior of the IIS / ARR URL rewriting function?
Is there any work-around for this behavior, such that the IIS / ARR could recognize -- and rewrite -- multiple URLs on the same line?
The solution for me was to ensure Regex doesn't match anything outside of the quotes, i.e. stop it from being greedy.
Using the GUI the match pattern ends with ([^"]*) instead of (.*), and when saved in the Web.config this gets escaped to ([^"]*)
The line in my Web.config looks like this:
<match filterByTags="None" pattern="http://your.domain/~/media([^"]*)" />
So I think it's safe to assume you're referring to an outbound rewrite rule to change URLs in HTML responses.
No that is not the default behaviour of the rewrite module.
Update your question to include your rule configuration so that we can help.
Useful information:
In your <match> element if you do not specify the filterByTags, then the match pattern will be applied on the entire response content, regardless of lines and occurrences. Note that the evaluation of regular expression patterns on the entire response content is a CPU intensive operation and may affect the performance of the web application.
It sounds to be that your rule(s) aren't properly configured.
Further information:
URL Rewrite Module 2.0 Configuration Reference
Creating Outbound Rules for URL Rewrite Module
I need ur help for given subject.
I am playing with htaccess rules first time in life.
here is the scene -
i want to redirect the urls -
http://www.abc.com/var1
http://www.abc.com/var2
to follwing urls -
http://www.abc.com/index.php?u=var1
http://www.abc.com/index.php?u=var2
In this case the values var1 & var2 can be anything (i.e. string which will contain only alphanumeric characters.)
One more thing -
I want to skip this rule if the url is -
http://www.abc.com/SKIPME
Please help me to write this rule!
Regards,
Shahu!
You would be better off defining a URI schema that tells you when something WILL be rewritten. For example...
http://www.abc.com/site/var1
http://www.abc.com/site/var2
That way, you can ensure you only ever apply the rule if the psuedo "site/" directory is browsed and not affect any other URI. This is the rewrite rule based on the above schema.
RewriteEngine on
RewriteRule ^site/([^/\.]+)/?$ index.php?u=$1 [L,NC,QSA]
Any other address, other than "/site/.../" would be unaffected by this rule, which means you don't need to worry about setting some addresses to be avoided. This keeps things as simple as possible.
You don't have to use "site" - you can use whatever name fits your purpose.