Htaccess forward with query params - .htaccess

I'm struggling with forwarding one service call with query params:
i want this:
https://myexample.center/myServlet?service=MyService&param1=a&param2=b&param3=etc
to be forwarded to:
http://myexample.center:8080/myServlet?service=MyService&param1=a&param2=b&param3=etc
Other calls to https://myexample.center/SDServlet?service=sthElse should not be affected
i'm grateful for any hint

For anyone who is curious how i solved it:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} service=MyService
RewriteRule ^(.*) http://myexample.center:8080/%{REQUEST_URI} [R=302,NC]
Working example for the same can be tested at .htaccess tester

Related

.htaccess - Rewrite https://www.sub.domain.com to https://sub.domain.com

I got a .htaccess issue, and I don't know why, because it should be working.
What I'm trying to do with this code is redirect https://www.sub.domain.com to https://sub.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ https://sub.domain.com/$1 [L,R=301]
When I test this code at http://htaccess.madewithlove.be/, it works succesfully.
(Request url: https://www.sub.domain.com, output url: https://sub.domain.com)
Also as mentioned in this question, I checked my DNS records , and there is an www.sub which points to the some domain as sub (both A's).
At this point I'm stuck, does anyone know what I did wrong?
Any help would be appreciated.
Create a file called .htaccess in the doc root (web / httpdocs / www folder) of https://www.sub.domain.com and put in the code below
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) https://sub.domain.com/$1 [R=301,L]

how to achieve subdomain url rewrite using modrewrite

I want to redirect website user to www.mywebsite/users.php?user=xyz when the user types http://xyz.mywebsite.com into the address bar where http://xyz.mywebsite.com is virtual and doesnt exist.
I am a beginner and doesnt know much about url rewriting. Tried to search google and stack overflow but didnt got the solution.
I started with this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+).mobilehealthnig.com$
RewriteRule (.*) http://www.mywebsite.com/users.php?subdomain=%1
</IfModule>
also tried this but none of these are working for me
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mywebsite.com
RewriteCond %{HTTP_HOST} ([^.]+).mywebsite.com
RewriteRule ^(.*)$ /users.php?subdoamin=%1
Ok, based on your additional input I suggest this setup:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.mywebsite\.com$
RewriteRule (.*) http://www.mywebsite.com/users.php?subdomain=%1 [L]
</IfModule>
If that does not work (I take that from your comment), you have to check where the actual problem is:
does rewriting work at all?
does this rule do anything atall? what?
is that rule ignored? why?
What absolutely makes most sense in such cases is to turn on rewrite logging to see and understand what is actually happening inside the rewrite engine. Please take a look at the two commands RewriteLog and RewriteLogLevel inside the manual mentioned. Enable a log level of maybe 7 and look what is logged when you make a single request. It should give you an idea of where to look for the cause of the problem.

How to create virtual username.domain.com using .htaccess mod rewrite

I know there are plenty of questions like I asked, but searching all day the soluation, I didnt find right one that would work for me, therefore I asked question.
Here is .htaccess code I have to view user profiles:
RewriteRule ^users/([^/\.]+)/$ viewProfile.php?user=$1&%{QUERY_STRING}
RewriteRule ^users/([^/\.]+)/([^/\.]+)/$ viewProfile.php?user=$1&usr_profile=$2&%{QUERY_STRING}
And using this rewrite users profile URL is:
http://www.domain.com/users/username.html
Inside of this type of URL, I would like to create subdomain users URL, like:
http://username.domain.com
Can anyone suggest the solution?
Thanks a lot.
Make sure your vhost is setup to accept requests for *.domain.com. Then add this above your other rewrite rules:
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteRule ^$ viewProfile.php?user=%1 [L,QSA]

Mod_Rewrite RewriteRule issue

I have a domain at example.com
There is a subdirectory that has a quiz on it, located at example.com/quiz/?id=1
I need to change the ?id=1 to TakeTheQuiz so it would look like example.com/quiz/TakeTheQuiz
Here is what my .htaccess looks like right now (the .htaccess is located in the root direct at example.com). Right now I always get a server 500 error.
RewriteEngine On
RewriteBase /quiz
RewriteRule ^?id=1$ TaketheQuiz
This is really simple and all of the examples I have seen have been really complicated and hard for me to apply it to this one :( Help, anyone? Thank you for your time.
You've just got the rule the wrong way round:
RewriteEngine On
RewriteBase /quiz
RewriteRule ^TaketheQuiz$ ?id=1 [L]
EDIT
Per your comment try this instead:
RewriteCond %{QUERY_STRING} id=1
RewriteRule ^$ TaketheQuiz [R=301,L]

htaccess mod_rewrite with dynamic parameters

I am new to mod_rewrite. I am trying to forward a URL to another one, but I cannot get it to work.
Say I want to forward this URL:
/cansas.php?m=2&id=2-0-0-0&sid=cansas to
/cansas-is-good-for-you and let the header respond with a 301, or just update the URL through [R].
I have this in my .htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^cansas.php?m=2&id=2-0-0-0&sid=cansas$ cansas-is-good-for-you [NC,R=301]
I figured I could just do a simple forwarding, but somewhere along the way it stops working. If I cut out the ?m=2&id= etc, it forwards just the cansas part to the new part so it looks like this: cansas-is-good-for-you?m=2&id=2-0-0-0&sid=cansas.
How can I forward it when I have several dynamic parameters in the URL string? Example on pages I need to forward:
/cansas.php?m=2&id=2-0-0-0&sid=cansas
/cansas.php?m=2&id=2-1-0-0&sid=cansas
/cansas.php?m=2&id=2-2-0-0&sid=cansas
Any help would be greatly appreciated :)
Maybe it isn't possible to do it this way? The way I have set it up at the moment is that I want to use new URLs called /cansas-is-good-for-you which reads from the source /cansas.php?m=2&id=2-0-0-0&sid=cansas, but the URL shown in the browser should be: /cansas-is-good-for-you. I need to forward that old cansas.php? URL to the new URL :)
You need to check the query of a URL with the RewriteCond directive as the RewriteRule directive only handles the URL path:
RewriteCond %{QUERY_STRING} ^m=2&id=2-0-0-0&sid=cansas$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]
If you want to check for just one parameter, use this:
RewriteCond %{QUERY_STRING} ^([^&]*&)*sid=cansas(&.*)?$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]
And to do this just for initial requests, you need to check the request line:
RewriteCond %{THE_REQUEST} ^GET\ /cansas\.php
RewriteCond %{QUERY_STRING} ^([^&]*&)*sid=cansas(&.*)?$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]

Resources