I need some help with rewriting an url to lowercase.
So, i have an apache server which is a proxy for a couple of applications (some on IIS and some on JBoss). The JBoss app has app context.
The problem is if i try to access an app over https://www.domain.com/App i get a 404.
If i try https://www.domain.com/app all is fine.
If i do a catch all RewriteRule to lowercase all uppercase then all my links and scripts get lowercase which is not really working.
In that case, a https://www.domain.com/app/SomeFolder/SomeScript.js link, would look like this https://www.domain.com/app/somefolder/somescript.js.
What i want is, if a https://www.domain.com/ApP/SomeFolder/SomeScript.js url is entered, to change it to https://www.domain.com/app/SomeFolder/SomeScript.js
My Apache config to catch uppercase letters and lowercase them is:
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} ^(/[A-Z]+/?)
RewriteRule ^([^/]*/?)(.*)$ ${lc:$1}$2 [R=301,L]
But this doesn't work and i can't seem to find the correct regex for the cond to catch only the app context and not the whole REQUEST_URI as my log says not matched.
Hope all this made sense. If more info is needed i will provide it.
Ty again.
EDIT:
The (?<=.com/)[^/\s]+ regex catches what i need. The question now will Apache work with this regex.
SO, the new ruleset should look like this(?):
RewriteMap lc int:tolower
RewriteRule ((?<=\.com/))([^/\s]+) ${lc:$1}$2 [R=301,L]
EDIT2:
Since i got no more feedback on this, i went and tried some other methods, and i ended up with this:
RewriteCond %{REQUEST_URI} !^/[A-Za-z]+/
RewriteCond %{REQUEST_URI} !^/[^a-z]+/
RewriteRule ^(.*)$ $1/ [R=301]
RewriteCond %{REQUEST_URI} ^/[A-Za-z]+
RewriteCond %{REQUEST_URI} ^/[^a-z]+
RewriteRule ^(.*) ${lc:$1} [R=301]
RewriteCond %{QUERY_STRING} "ReturnUrl="
RewriteRule ^(.*) $1? [R=301]
This does all i want except lowercase an uppercase URI when it doesn't start with an uppercase. So a /APp uri will get rewritten to /app but /aPp won't be. I'll just leave this here, hopefully i'll get some feedback on this.
One problem now is that sometimes the loading takes >15 seconds. And i can only relate this to the rewrite having, in the worst case, 3 301 redirects...
find the correct regex for the cond to catch only the app context
this regex will grab everything after "app/":
(?<=app/)[\S]+
now each whole match can simply be fed to a "toLowerCase()" function equivalent.
regex demo
Related
I want to feed several URLs into a single php file that will handle the contents of the page, the URLs are like
domain.com/fashion-registration
domain.com/singing-registration
I want to capture URLs ending with -registration and feed fashion or singing into the page but it doesn't seem to be working. This is what I tried
RewriteRule ^(.*)$-registration category.php?link=$1 [NC,L,QSA]
Could you please try following.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/(.*)-registration/?$ [NC]
RewriteRule ^(.*)$ /category.php?link=%1 [NE,NC,L]
OR you could try following too, one without RewriteCond.
RewriteEngine ON
RewriteRule ^(.*)-registration/?$ /category.php?link=$1 [NE,NC,L]
Problem in OP's attempt: Since you have used ^(.*)$ and after that you are using -registration in your regex that's why your regex is NEVER going to match -registration
I am sorry to ask a so trivial question, but I cannot find answers in RewriteRule docs. It should be something very simple that I am missing here...
I need to redirect
/index.php/component/users/?view=login
to
/index.php
if the URL matches "view=login"
I tried
RewriteRule ^((.*)\?view=login(.*))$ /index.php? [R,L]
and it doesn't work though testing it on www.regextester.com or www.regexpal.com shows a match
At the same time matching the substring "users" works perfectly fine using
RewriteRule ^(.*)/users(.*)$ /index.php? [R,L]
What is the problem with my regex matching "view-login"? Why does it work on a tester and doesn't on a live site?
Thanks!
UPDATE
I have managed to get it work using RewriteCond and RewriteRule:
RewriteCond %{QUERY_STRING} login
RewriteRule ^(.*)$ /? [R,L]
Still I cannot understand why a single RewriteRule doesn't work to match string after "?". It works only for the part of the string before "?".
I would appreciate if someone could explain that. Thanks!
setting url rewriting to have nice urls, i have existing urls like that :
/xxx/test.php
but in the background, it is allways going to the same script with a query :
/xxx/index.php?id=test
with the following rewrite :
RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=$1 [QSA,L]
it's working fine.
now, there are old urls still like /xxx/index.php?id=$1
and i want to get rid of these old urls, meaning I want all of them to be for the users like /xxx/test.php with a 301 redirect
i did a rewrite for this but then i'm entering a loop despite the L flag
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^xxx/index\.php$ /xxx/%1.php? [R=301,L]
? is it possible to handle that and how ?
and other to describe it is allways use the script :
/xxx/index.php?id=$1
but allways have the right url in the browser displayed
Keep your existing
RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=$1 [QSA,L]
which appears to work fine.
Add in these two lines before that which will catch if there is an id= and strip it out of the URL.
RewriteCond %{QUERY_STRING} ^id=([^&]*)(.*)$
RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=%1%2 [L,R=301]
^ start of query string
([^&])* any character except &
(.*) any following characters
So if query string is id=test&something=else RewriteRule will append exactly that and nothing else as there is no more QSA flag.
Try those 3 lines together (htaccess test website), here is the full htaccess file:
RewriteCond %{QUERY_STRING} ^id=([^&]*)(.*)$
RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=%1%2 [L]
RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=$1 [QSA,L]
Make your RewriteRule not match index.php or remove the QSA flag.
Say you type test.php well now you will go to index.php?id=test
Then Rewrite occurs again and you will go to index.php?id=index&id=test
Then it will occur again because the page is different: index.php?id=index&id=index&id=test etc.
So add in your regex a negative lookahead: xxx/(?!index)([0-9a-z\-]*)\.php
Try:
RewriteRule ^xxx/(?!index)([0-9a-z\-]*)\.php$ /xxx/index\.php?id=$1 [QSA,L]
I'm trying to redirect URLs with .htaccess in order to be able to make this:
request: http://www.example.org/business/businesswhatever/
redirect: http://www.example.org/businessgrowing/businesswhatever/
The more close I am of reaching the solution is to apply this code:
RewriteRule ^(.*)example\.org/businessgrow/(.*)$ $example.org/businessgrowing/$2 [R=301,L]
but it does not work at all. Validator doesn't check a thing, the only way to validate is if I omit http:// part of the URL.
I need to use hostname as part of the regex because some changes were made and now businessgrow is inside this path: www.example.org/corp/businessgrow, so I need to take part of the hostname in the regex in order to distingish between example.org/businessgrow (it must be a 404) and example.org/corp/businessgrow (200 OK)
I'm using this checker http://htaccess.madewithlove.be/ in order to test before deploying the solution. I tried to remove a slash from http:// (because I was unable to think anything else, I already tried anything) and it worked, but validator (I mean, it doesn't skip validation) outputs 3 slashes like this http:///www.example.org/en/businessgrow/businesswhatever/
If I input the URL without http:// code above works perfectly, but Apache doesn't recognise it and website is unable to show a single page.
I'm using Debian wheezy server and I'm using Wordpress, i took care of write this line just below REwriteBase line, so my .htacces is like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)example\.org/businessgrow/(.*)$ $1example.org/businessgrowing/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I would appreciate if anybody gives me a clue in order to solve this, I think there's some mistake with validation process but I can't find it.
Thanks so much for your time!
btw, sorry about my english..
You don't want the hostname as part of the regex, only the request URI path is used to match. Try:
RewriteCond %{HTTP_HOST} example\.org$ [NC]
RewriteRule ^businessgrow/(.*)$ /businessgrowing/$1 [R=301,L]
I'm trying to make nice links in my apps.
I decided to rewrite links that look like these:
1. http://example.cz/get1/get2/get3
2. http://example.cz
Into these (I have php appliactions only):
1. http://example.cz/index.php?path=get1+get2+get3
2. http://example.cz/index.php?path=
I'm removing www before links.
I keep failing to rewrite it into .htaccess.
I'm also looking for advice if the primary idea of rewriting get params into path=get1+get2+get3 is good? Right now I can see that link like this http://www.example.cz/you+me/ could possibly fail somewhere. Do you have any better solution?
So question is: How to rewrite it into .htaccess and how to solve possible problems with link that contains '+'
EDIT:
I improved my skills a little and I did this:
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule (.*) http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^\/index.php(.*)
RewriteRule ^(.+) /index.php?path=/$1 [R=301,L] # 301 is here so I can see how does it work
# everything above works well (as I want)
# link now look like this one:
# http://example.net/index.php?path=/get1/get2/get3
# Now I was looking for universal rule that will rewrite my get params...
# First I did this:
RewriteCond %{REQUEST_URI} /([^/]+)
RewriteCond %1 !index.php(.*)
RewriteRule /([^/]+) $1+ [R=301,L]
# If any part of request uri is string that matches pattern /([^/]+)
# And if ([^/]+) doesn't match index.php(.*)
# then rewrite every /([^/]+) into $1+
# Now I see that it is incorrect, but I wasn't able to fix it
# So then I did different rule
RewriteRule ^([^=]+=[^\/]*)\/([^\/]+)(.*)$ $1$2+$3 [R=301,L]
# start of string
# first var is: one or more chars except =, =, zero or more chars except /
# /
# second var is: one or more chars except /
# third var is: zero or more chars
# end of string
I think second idea was much more better, but it doesn't work too. Please help me to fix it.
You can do this with the Apache module mod_rewrite. Chances are you probably already have it installed. Try this:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?path=$1+$2+$3 [L]
This regex assumes the URL will always contain three groups of text between slashes. You can tweak it as needed.
Also note that Apache never sees the URL hash, so you won't be able to match it in a rewrite rule. Luckily, it looks like you don't want to do anything with it anyway. Just use the rule above, and the hash will remain at the end of the URL in the browser.
I did solution. Problem was that after adding index.php?path= I wasn't able to work with query string...
The final universal solution that turn links from http://www.example.net/get1/get2/get3 to
http://example.net/index.php?path=get1+get2+get3:
RewriteEngine on
RewriteBase /
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule (.*) http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^\/index.php(.*)
RewriteRule ^(.+) /index.phppath=/$1 [R=301,L]
RewriteRule ^([^=]+=[^/]*)/([^/]+)(.*)$ $1$2+/$3 [R=301,L]
RewriteRule ^(.*)\+/+$ $1 [R=301,L]
RewriteRule ^(.*)path=(.*)$ $1?path=$2 [R=301,L]