how to achieve subdomain url rewrite using modrewrite - .htaccess

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.

Related

How do I redirect to all pages to be a subdirectory of another website but keep the URLs the same?

I have a website that has been built at say montypython.netlify.app
The client has their main website at holygrail.com and they want holygrail.com/resources to show the contents of montypython.netlify.app but keep the URL the same. Which means that it should continue to show holygrail.com/resources in the search bar.
This also means that any pages from montypython.netlify.app should appear are subdirectories of holygrail.com/resources
Example:
montypython.netlify.app/about should appear as holygrail.com/resources/about
I am guessing this has to do with editing the .htaccess at holygrail.com but what rewrite/redirect rules can I reference? There are a lot of URLs so is there a wildcard approach I can use?
This is what I've tried:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^holygrail\.com$ [NC]
RewriteRule ^resources/(.*)$ https://montypython.netlify.app/$1 [R=301,L]
</IfModule>
You can use the [P] proxy flag of mod_rewrite.
Using [P] flag instructs mod_rewrite to handle the request via mod_proxy. Therefore, you must enable mod_proxy to use flag.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^holygrail\.com$ [NC]
RewriteRule ^resources/(.*)$ https://montypython.netlify.app/$1 [P]
</IfModule>
with this code snippet, all pages to be a subdirectory resources will be served from https://montypython.netlify.app/ without a 301 redirection.
Maybe your issue is with the line:
RewriteCond %{HTTP_HOST} ^holygrail\.com$ [NC]
Maybe you need to try to do something like
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^resources/(.*)$ https://montypython.netlify.app/$1 [R=301,L]
</IfModule>
I have worked on projects in the past with similar objectives. I don't believe you can accomplish this with redirects. The suggestion about using a reverse proxy would be the most well aligned with your requirements, but there is another option that may also be useful. Some DNS providers offer "DNS Cloaking" or "Stealth Redirects". This can be configured so that requests for holygrail.com will display a frame containing the content for montypython.netlify.app. Could you use the same approach for the /resources sub-directory, so that holygrail.com/resources delivers a frame that loads montypython.netlify.app?
The drawback to this is the address bar will not change as you navigate inside the frame, e.g. navigating to montypython.netlify.app/resources/about will still show holygrail.com/resources in the address bar, because it is displaying the address of the frame.

mod_rewrite strange infinite recursions

Server is Apache 2.4.18, PHP ver 7.0...
Intro:
On php side I rawurlencode($name) when creating html link.
REQUEST_URI looks like this:
/lang/cat1/cat2/product with bla (this/that) -- works
/lang/cat1/cat2/product with bla (this/ that) -- infinite internal redirect
The difference is only in one space this/ that vs. this/that
VirtualHost:
# I have tried different combinations of
AllowEncodedSlashes On
AllowEncodedSlashes NoDecode.
htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews #different combinations
RewriteEngine On
# tried different combinations
#RewriteCond %{REQUEST_URI} !^/?file\.php$
#RewriteCond %{ENV:REDIRECT_STATUS} ^$
#RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule .? file.php [L] # END
</IfModule>
All I want is standard front controller, ALL requests should go to file.php That works in 99.99% of cases. But ruri with bla/ in it goes to infinite recursions.
Questions:
Why is this happening?
How to solve it?
You need to uncomment your last rewrite condition.
Try :
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews #different combinations
RewriteEngine On
# tried different combinations
#RewriteCond %{REQUEST_URI} !^/?file\.php$
#RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{ENV:REDIRECT_STATUS} !200
# END
</IfModule>
Otherwise your rule will redirect /file.php to itself.
On apache 2.4 you can simply use END flag to terminate the rewrite processing :
RewriteEngine on
RewriteRule .? file.php [END]
I have found a solution on another forum.
Solution was painfully simple. But might help someone.
One guy sad: Be brave, understand that there is nothing wrong with your htaccess! But you still have recursions. So bug is somewhere in php code.
That destroyed my htaccess fixation allowing me to dive into business and fw code. Bug was found few hours later. One of url functions had premature rawurldecode(). Cool for lazy programin, not so cool if you have products with encoded / in names.
But the answer to question "Did this work before?" was misleading also. "Yes, sure. It must be because of resent migration."
Riiight ;)

.htaccess redirect from one subfolder to other subfolder

I know this sounds like so many other questions here, but I can't seem to find the answer.
Say you are on:
www.domain.com/folderA/folder2/folder3/
I want that to redirect to:
www.domain.com/folderB/folder2/folder3/
So the whole structure stays the same.. it just redirects.
Now so far I have:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/folderA [NC]
RewriteRule ^(.*)$ /folderB/$1 [R=301,L]
But when I use that, it'll just do
www.domain.com/folderB/folderA/folder2/folder3/
What am I doing wrong? How do I get rid of that folderA?
The pattern ^(.*)$ includes also the prefix folderA. You must specify folderA explicitly in the pattern and capture only the latter part in the RewriteRule. Then you can drop the RewriteCond
RewriteEngine on
RewriteRule ^/?folderA/(.*)$ /folderB/$1 [R,L]
Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

.htaccess redirect to a single page everytime, but keep same url for application reasons

I looked through other stack overflow questions and I couldn't find one that was my exact case.
I have tried writing several different .htaccess rewrite rules but I can't seem to get it working.
I need to do the following:
Original URL: testexample.com/tool/1
Needs to redirect to: textexample.com/tool/display.php
But the URL in the browser needs to stay : testexample.com/tool/1
Can anyone point me in the right direction for rules for this rewrite?
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !display\.php/? [NC]
RewriteCond %{REQUEST_URI} ^/([^/]+)/?.*/? [NC]
RewriteRule .* %1/display.php [L]
Maps silently
http://testexample.com/anything/folder (Shown in the browser all the time)
To
http://textexample.com/anything/display.php

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]

Resources