I have several domains that I want to all rewrite to one domain. I don't want it to redirect because I want the URL to look like what the user has entered in. For example if they enter www.example.com I want it to load the page from www.sample.com/default.php?from=example
I have worked a little with rewriting if you have www.site.com/var1/var2 making it load www.site.com/index.php?one=var1&two=var2
Is it possible to do what I am looking for just through the .htaccess file? I tried looking around and couldn't exactly find what I was looking for
Thanks
If the sites are hosted on different servers or don't share a common document root, then you'll have to rely on mod_proxy and you can use the P rewrite rule flag. For example, these rules in an htaccess file in www.example.com's document root:
RewriteCond %{HTTP_HOST} ([^.]+)\.com$ [NC]
RewriteRule ^/?$ http://www.sample.com/default.php?from=%1 [L,P]
Will take the request http://www.example.com/ and invisibly proxy it to http://www.sample.com/default.php?from=example. The browser's URL address bar will remain http://www.example.com/.
Note that the rule only matches against the request URI /. If you want to do more, you'd have to create the correct regular expression and grouping.
If you have redirects on the sample.com site, you'll need to employ ProxyPassReverse to rewrite the redirects. Also see ProxyPassReverseCookieDomain and ProxyPassReverseCookiePath if there are cookies involved.
If you can do this in vhost or server config instead, then consider simply using ProxyPass instead of mod_rewrite. The ProxyPass directive won't work inside htaccess files.
EDIT:
Seeing as how everything is in the same document root, you won't need to proxy anything. Simply:
RewriteCond %{HTTP_HOST} !^www\.sample\.com$ [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.com$ [NC]
RewriteRule ^/?$ /default.php?from=%1 [L]
I use online generators.
Use this generator: mod-rewrite
Related
I am having an issue with a subdirectory that has many different paths, but on the new website we are no longer using that structure. The old website worked this way:
example.com/photos/photo-1
example.com/photos/photo-2
example.com/photos/photo-3
On our new site we are not using /photos, and my current redirect attempts have not worked
Redirect 301 /photos https://example.com/
ends up redirecting to
example.com/photo-1
example.com/photo-2
example.com/photo-3
Removing the photos subdirectory, but still attempting to access the subdirectory that follow it.
The desired result would be for any attempt to access example.com/photos or example.com/photos/* should redirect to homepage at example.com/
Any help would be appreciated.
This should do it.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^photos/(.+) https://example.com/ [R=301,L]
</IfModule>
This probably is the approach that makes most sense:
RewriteEngine on
RewriteRule ^/?photos/[^/]+/(.+)$ https://example.com/$1 [R=301]
It uses the rewriting module which means that has to be loaded into the http server. That is the standard setup, though.
In case it is not all subfolders that should get redirected you can name a pattern instead. This allows for exceptions. For example:
RewriteEngine on
RewriteRule ^/?photos/photo-\d/+(.+)$ https://example.com/$1 [R=301]
And in case both host are identical for the old and the new "site" you can use an internal redirection to simplify things:
RewriteEngine on
RewriteRule ^/?photos/photo-\d/+(.+)$ /$1 [R=301]
In general you should try to implement such rules in the actual http server's host configuration. And only use a distributed configuration file instead (".htaccess") if you have no access to the central configuration. Various reasons.
I have a folder structure like this
/
/project1
I normally use it with one domain (example.com), but now I want to use it as a sepreate domain (project1.example.com) and I am using apache virtualhost.
I have succeed to setup, but some problem on the link happened.
All the hyperlink in project1 is the path included the "project1" subfolder (e.g. project1/image/pic.jpg).
I setup like this
DocumentRoot /doc_root/
Alias /project1 /doc_root/project1/
Alias / /doc_root/project1
RewriteRule /project1/(.*) /$1 [R]
The problem is when i rewrite clean url
e.g. example.com/project1/post?id=1 => project1.example.com/post/1
With
RewriteRule ^/post/(\d+)$ /project1/post.php?id=$1 [PT,NC,L,QSA]
I am getting 404 error in the ajax post in the page
Since the ajax url is pointed to /project1/process.php
And I think this request is not rewriting.
What is the proper way to setup like this? thanks!
There's several issues here:
The Alias rules don't look correct and are confusing.
The redirect rule you provided won't extract a query string parameter (i.e. it won't rewrite the path from /project1/post?id=1 to /post/1) nor does it alter the domain name (i.e. it won't change the domain to project1.example.com).
There's no need for the redirect.
I think what you want is something like:
DocumentRoot /doc_root/
RewriteCond %{HTTP_HOST} ^project1\.example\.com$
RewriteRule ^/(.*)$ /project1/$1 [NC,PT]
If you do actually want to redirect, consider:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^/project1/(.*)$ project1.example.com/$1 [NC,R]
I find a lot lot lot questions about redirection with htaccess.
However it seems no one has the same "problem" as me.
What I want is redirect if a directory is accessed, it should redirect to the subdomain. So far its good and working. However, I dont want to change my htaccess all the time when a domainname is changed.
So I have:
RedirectMatch 301 ^/subdir/(.*)$ http://subdir.example.com/$1
But in my case I put my application on the domain example2.com, I have to change the name.
Is it possible to have something like this:
RedirectMatch 301 ^**(capture_hostname)**/subdir/(.*)$ http://subdir.**(put_hostname_here)**/$1
All "solutions" seems to work with defining the domainname in htaccess.
I work not enough with htaccess to solve this issue. Therefore we are together :)
You can do it with mod_rewrite rules and capture hostname from a RewriteCond:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(subdir)(/.*)?$ http://$1.%1$2 [L,NC,R=301,NE]
%1 is the value being captured from RewriteCond directive, $1 is subdir and $2 is part after subdir/
I have a subdoman called, let's say:
cloud.mygizmo.com
But when someone navigates to this URL I want them to actually go to:
11.22.33.44/cloud
Which is on a completely different host from mygizmo.com and can't be moved.
In my .htaccess I have this:
RewriteEngine on
# Use PHP5.4 as default
AddHandler application/x-httpd-php54 .php
RewriteCond %{HTTP_HOST} ^cloud\.mygizmo\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.cloud\.mygizmo\.com$
RewriteRule ^/?$ "http\:\/\/11\.22\.33\44\/cloud" [L]
Which does do the redirect, but it still changes the address bar in the user's browser.
How do I make it so that if a user navigates to cloud.mygizmo.com they actually go to 11.22.33.44/cloud but the address bar still says cloud.mygizmo.com?
You can't. Redirection doesn't work like that.
You could proxy the data instead (which would be inefficient and increase your bandwidth costs).
If you have mod_proxy installed, you can use the P flag to reverse proxy on behalf of the browser:
RewriteEngine on
# Use PHP5.4 as default
AddHandler application/x-httpd-php54 .php
RewriteCond %{HTTP_HOST} ^cloud\.mygizmo\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.cloud\.mygizmo\.com$
RewriteRule ^/?$ "http\:\/\/11\.22\.33\44\/cloud" [L,P]
You can also reverse proxy using ProxyPass or ProxyPassMatch but those will only work in the vhost/server config.
In the cloud.mygizmo.com/www.cloud.mygizmo.com vhost you can say:
ProxyPass / http://11.22.33.44/cloud
And then any request for the cloud.mygizmo.com gets proxied to the http://11.22.33.44/cloud host.
Note that ProxyPass works like Redirect, it links together the path nodes / and /cloud. So if someone were to go to:
http://cloud.mygizmo.com/foo/bar
They'd get reverse proxied to:
http://11.22.33.44/cloud/foo/bar
If that's not what you want, then use ProxyPassMatch:
ProxyPassMatch ^/$ http://11.22.33.44/cloud
Alternatively, if you want the rewrite rule to behave in the same way, you need to capture the request URI and pass it to the target with a backreference:
RewriteRule ^/?(.*)$ http://11.22.33.44/cloud/$1 [L,P]
probably not the ideal solution, but you could build a page that's pretty much a giant iframe and load the content inside the iframe...
Edit: See if Blue Host allows Parked Domains in your control panel (aka Masked Forward). I think this is what you want.
I am sorry, I have tried for hours to get this working, but I haven't made progress...
I want to make it so that if a user on my site types user.site.com they will be taken to site.com/user, but the URL will still show user.site.com. How can I do this? With .htaccess? Server files?
Almost there Ken
RewriteEngine On
RewriteCond %{HTTP_HOST} !www.site.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).site.com [NC]
RewriteRule (.*) %1/$1 [QSA,L]
%1 = what's before .site.com
$1 = what you got after the /
If you have test.site.com/foo.php , you would have /test/foo.php.
if you just want test, just forget about the $1.
QSA = query string append,
L = Last.
You should read the url about mod_rewrite in #phihag post.
Use mod_rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.site.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).site.com [NC]
RewriteRule (.*) %1/$1 [QSA,L]
If you want to link to resources, either use full (http://site.com/user/static/x.css) or relative (static/x.css) URLs. Absolute URLs (/user/static/x.css) will need to be crafted differently when this rule is in effect.
To keep the original address in the address bar you will need a reverse proxy rather than a redirect. Redirecting tells the browser to send a second request to the server with a different address, reverse proxy tells the server to find another page and send it without notifying the browser about it (this is what you want I believe). Reverse proxy is achieved with the [P] flag in mod_rewite
Make sure mod_rewrite, mod_proxy and mod_proxy_http are loaded and put the directives
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
RewriteEngine on
RewriteRule ^/(.*) http://site.com/user/$1 [PL]
into your virtual host configuration for user.site.com or .htaccess if you do not have root privileges. This will proxy all pages from the subdomain to the main domain folder. If you only want to proxy the index page use RewriteRule ^/ http://site.com/user instead.
I assume you are using http and not https. If so, it gets a little more complex...