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!
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 need to redirect any requests with query strings from a set of origin URLs back to a thank you page.
For example, I need to redirect:
http://example.com/test1/test2/[origin]/?id=1
back to
http://example.com/thank-you
The way I've got it set up in my .htaccess file is as such:
RewriteEngine On
RedirectMatch 302 ^/test1/test2/(.*)/.+ /thank-you
I've tested the regex I'm using in an online regex tester and it appears to work as expected, so I'm confused as to why the redirect isn't taking place. Here's the link to that.
Obviously, I had to add backslashes to escape the slashes in the URL in the regex tester, but based on my understanding of how .htaccess evaluates regex, these aren't necessary.
My question is: the redirect works perfectly from the page without the query string if I remove the .+ from the end of the regex string, meaning that the beginning part of the regex works fine. I don't understand why the query string isn't matching the regex I've created.
I have also tried:
RewriteCond %{REQUEST_URI} ^/test1/test2/(.*)/
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule (.*) /thank-you [R=302,L]
For your RedirectMatch, you may use:
RedirectMatch 302 ^/test1/test2/(.*)/(.*)+ /thank-you?
For your RewriteRule section, you may use:
RewriteCond %{REQUEST_URI} ^/test1/test2/(.*)/
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule (.*) /thank-you [R=302,L,QSD]
First , no need to RewriteEngine On with mod_alias which is RedirectMatch at your rules use it with mod_rewrite , the second rules .
Try this :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule ^test1/test2/[^\/]+/$ /thank-you? [R=302,L]
I use ^id=([0-9]+)$ to restrict query string for a one that start with id and end with numerical value.
I remove this line RewriteCond %{REQUEST_URI} ^/test1/test2/(.*)/ becasue you could match against URI in RewriteRule as well.
If this rules wrok , change [R=302,L] to [R=301,L] to be permanent redirection.
Note: clear browser cache then test
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
I am working on a website project where we basically move from TYPO3 to a WordPress & Magento-solution.
Before launching the new site, I would like to add rewrite rules to point the old (TYPO3, non-SEF) URLs to the corresponding new ones. I have an Excel-list with around 1000 URLs that I somehow would like to add to htaccess and create 301's.
If you have a better approach for this, I'd be thankful.
What I am struggling with is:
The "old" URL structure looks something like ?id=123\&user_e15proddb1_pi1[domain]=42
the correcponding new URL would be
/de/alle-produkte/neuheiten.html
RewriteEngine is on, RewriteBase is /.
I tried
RewriteCond %{QUERY_STRING} ^id=123\&user_e15proddb1_pi1[domain]=42$
RewriteRule . /de/alle-produkte/neuheiten.html [R=301,L]
With additionally escaping the _and the [] with no avail.
I tried to seperate the {QUERY_STRING}s into two by
RewriteCond %{QUERY_STRING} ^id=123$
RewriteCond %{QUERY_STRING} ^user_e15proddb1_pi1[domain]=42$
followed by TheRule. Also no avail.
Rewriting itself works, because I tried
RewriteRule .id=123\&user_e15proddb1_pi1\[domain\]=42$ /de/alle-produkte/neuheiten.html [R=301,L]
But that only works without the question mark in the beginning.
Could you give me a hint on what I am doing wrong?
You can use this rule by escaping [ and ]:
RewriteCond %{QUERY_STRING} ^id=123&user_e15proddb1_pi1\[domain\]=42$
RewriteRule ^ /de/alle-produkte/neuheiten.html? [R=302,L]
Also note ? at the end of target URI to strip off any existing query string to prevent a redirect loop.
I got a solution to and made this one work:
RewriteCond %{QUERY_STRING} ^id=123\&user\_e15proddb1\_pi1\[domain\]=42$
RewriteRule (.*) /de/alle-produkte/neuheiten.html? [R=301,L]
Proably something with escaping those characters was going wrong when I tried over and over again.
I just can't get my head around the regex required for a URL rewrite.
I want the following :
http://domain.com/?hid=yv&v=0.1.4383
to rewrite to
http://domain.com/example/
All other pages should remain unchanged.
Anyone help me out and explain the regex required for me.
You need to make sure to match against the %{QUERY_STRING} variable.:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^hid=yv&v=0\.1\.4383$
RewriteRule ^/?$ http://domain.com/example/ [L,R]