Problems redirecting old domain to new with Apache and htaccess - apache

My homepage is located at www.nazgulled.net and I bought a new domain which is www.ricardoamaral.net. Both these domains point to the same exact server.
I'm having two problems with the redirection, my current code is this:
RewriteCond %{HTTP_HOST} ^(www\.)?nazgulled\.net [NC]
RewriteRule ^(.*)$ http://www.ricardoamaral.net/$1 [L]
For now I'm just testing but the idea is to replace [L] with [L,R=301] when I'm ready to move the whole thing, just so you know.
Anyway...
1) This is not working, when I try it, I can't access "nazgulled.net", it gives me a "server internal error" and I don't understand why... I don't understand why because if replace "ricardoamaral.net" by "google.com", the redirect works just fine :/
2) I have a few subdomains and I would like to redirect everything in those too. My first choice is to add different rewrite conditions/rules for each of the subdomains but that takes a lot of manual code and if the user types some subdomain that doesn't exist, they don't be redirect it and I also want that.

I think for your situation creating separate vhosts would be the ideal fit. What I do often is place domains that are being redirected on the same server but in a different folder to keep the www folder clean, for instance. My main site would be here:
/var/www/example.com/public
For all my redirects I would place them like so:
/var/www/redirects/example-2.com/public
Within each public folder of the redirected sites you would add this line to your .htaccess file
RedirectMatch permanent /.* http://example.com/

For 1, you can use something like this instead:
<VirtualHost *:80>
ServerAdmin webmaster#nazgulled.net
ServerName nazgulled.net
ServerAlias www.nazgulled.net
Redirect permanent / http://www.ricardoamaral.net/
</VirtualHost>
I use a similar configuration and it works perfectly, keeping query parameters, paths, etc.
Regarding your own rewrite rule, I want to note that you may lose your query params if you don't include QSA.
To diagnose internal server errors, just have a look at the log file provided by your ErrorLog directive within your vhost, or a global one, depending on your configuration. It should give you the exact reason for the error.

This is best solutions. Create a notepad file and save it as .htaccess if you do not already have an existing one.
Update the .htaccess file with the following code and save
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://new-domain.com/$1 [R=301,L]

Related

Apache subdomain vhost + subfolders of several Magento 2 installs

Basically I want to have several stores under one subdomain as sub-folders. Like this:
mysubdomain.website.com/magento2v1 - Magento 2 store #1
mysubdomain.website.com/magento2v2 - Magento 2 store #2
mysubdomain.website.com/magento2v3 - Magento 2 store #3
Steps that I did:
This subdomain will be run in a VPS server so first I needed to redirect the subdomain (mysubdomain.website.com, the one above) to another IP (the actual VPS server) using an A record.
Second I created a Vhost with the mysubdomain.website.com on that VPS server
This is the vhost on the VPS server, Apache2:
<VirtualHost *:80>
RewriteEngine On
ServerName mysubdomain.website.com
ServerAlias www.mysubdomain.website.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/mysubdomain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And then in /var/www/mysubdomain I have:
/var/www/mysubdomain/magentov1
/var/www/mysubdomain/magentov2
/var/www/mysubdomain/magentov3
Now the problem is, Magento 2 needs .htaccess redirects where it redirects /pub to the actual sub-folder. I can't seem to get these .htaccess to work. The htaccess is read but it seems like the folders go a little bit crazy.
/var/www/mysubdomain/magentov1/.htaccess
/var/www/mysubdomain/magentov2/.htaccess
/var/www/mysubdomain/magentov3/.htaccess
Here's an example of this .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/pub/
RewriteCond %{REQUEST_URI} !^/setup/
RewriteCond %{REQUEST_URI} !^/update/
RewriteCond %{REQUEST_URI} !^/dev/
RewriteRule .* /pub/$0 [L]
DirectoryIndex index.php
Can anybody help me get this .htaccess to work? I think it's related to the folder structure, the folder base, but I don't know how to do this and didn't found any other example.
Thank you very much!
The issue most likely is that you rewrite to the absolute path /pub/ from within the configuration files in the subfolders. How should that rewritten request be processed? After that rewriting step it appears to the next round of rewriting like a request to https://sub.example.com/pub/........ None of your existing configuration files will get applied to that. And it most likely will lead to a http status 404. Which you should be able to see in your http server's error log file and also in your browser's console network tab.
Instead you should rewrite to the relative path pub/instead. Which would result in the next rewriting step getting applied to the request to https://sub.example.com/magentov2/pub/........ Which would again get (correctly) served from within that subfolder.
In general you should try to keep global rewriting rules in a common place instead of doubling them in various parallel configuration files. Even better than a common distributed configuration file (".htaccess") in the http hosts DOCUMENT_ROOT folder would be to implement such rule in the actual central configuration file. That is faster, more robust, more secure and easier to debug.

apache mod_rewrite user URLs to index.php

I've always had a fairly standard apache configuration. Right now we're introducing a new concept, user session specific URLs that's going to change things. Basically we have a DocumentRoot and anything such as:
http://example.com/ would hit index.html in the DocumentRoot directive.
But now I'd like to be able to do something like
http://example.com/uid/5/
http://example.com/uid/2
Those should still hit index.html in the DocumentRoot that has been set. The URL is mostly for server-side and client-side scripts to be able to carry out their own tasks.
What's the best way to handle this in Apache? Is mod_rewrite even necessary here?
I also need to be able to support existing paths such as say the following:
http://example.com/foo/bar/something.php will be rewritten to http://example.com/uid/3/foo/bar/something.php but will still hit the same place on the filesystem as before.
You could use mod_rewrite by putting this code in your htaccess
RewriteEngine On
RewriteRule ^uid/([1-9][0-9]*)/(.+)$ /$2?uid=$1 [L]
Example:
http://example.com/foo/bar/something.php -> unchanged
http://example.com/uid/3/foo/bar/something.php -> rewritten to /foo/bar/something.php?uid=3
EDIT: without uid appended
RewriteEngine On
RewriteRule ^uid/[1-9][0-9]*/(.+)$ /$1 [L]

Redirect entire directory as my home directory with mod_rewrite

I've been looking for an answer to this for a while and I've tried many tricks myself, but to no avail, so I bring my questions to all you wise people.
I have a website all ready to go. The entire contents of this website is in the main "www" directory of the site. So now when I go to www.mywebsite.com/main, it goes to the www/main.php file. However, I want to move the entire contents of my website into a new folder inside the www directory, which I will call "ts". So now, my main.php file will be in www/ts/main.php
However, the problem I am having is I want all URIs to remain the same, so I want to tell the server that www.mywebsite.com/main is now found at www/ts/main.php instead of where it was before (www/), and that all subsequent content is also now found in www/ts instead of www/.
I imagine that I will need a .htaccess file in my www/ directory which tells the server that the absolute path of www.mywebsite.com is found at www/ts/, but I am not sure how to do that. I also have an .htaccess file inside my new www/ts/ directory already redirecting many URIs, but I imagine that I need to tell the server that all the files within the directory need to ignore the /ts/ URI, again, I am not sure how to do that.
If anyone is able to point me in the right direction, I would be much obliged. Thanks.
EDIT: I am on a shared server so I do not have access to any of the conf files, therefore I need a solution involving mod_rewrite instead. Thanks.
You just need to specify where is the DocumentRoot of your VirtualHost.
Check Apache configuration for your Virtualhost (it's maybe the default one, but you could create one for your named site). And check the DocumentRoot tag.
If it was something like:
DocumentRoot /foo/bar/www
Alter it to :
DocumentRoot /foo/bar/www/ts
And that's all.
Edit:
With the restriction of only accessing a .htaccess and if we assume you will limit this new folder redirection for only one DNS name (as if you make a subdir it's maybe to make some other subdirectories later) this should work:
RewriteEngine On
# limit this rule on the www.mywebsite.com DNS name requested
RewriteCond %{SERVER_NAME} ^www\.mywebsite\.com$
# limit infinite recursion, when filename is ok...
# no, in fact not # RewriteCond %{REQUEST_FILENAME} !-f
# well this one is better, no loop for mod_rewrite, even on 404
# the rule is applied only once
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# so here's the rule, map the requested file on the filesystem in ts subdir
RewriteRule ^(.*)$ /var/www/ts/$1 [L]

Redirect non-www to www not working

I set up a virtual server using virtualmin, it didn't create the .htaccess file so I created one in the public_html folder and put the following code
RewriteEngine On
RewriteCond % ^megahotserved.com [NC]
RewriteRule ^(.*)$ http://www.megahotserved.com/$1 [L,R=301]
restarted apache and no effect and then tried
<VirtualHost *:80>
ServerName megahotserved.com
Redirect permanent / http://www.megahotserved.com/
</VirtualHost>
in the httpd.conf file, when I restarted apache firefox came up with an error
The page isn't redirecting properly.
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
What should I do ?
your code is correct, you just need to follow the next paragraph which I quoted from http://httpd.apache.org/docs/current/mod/mod_rewrite.html
By default, mod_rewrite configuration settings from the main server context are not inherited by virtual hosts. To make the main server settings apply to virtual hosts, you must place the following directives in each section:
RewriteEngine On
RewriteOptions Inherit
Seems like you don't have a VirtualHost that properly matches the www. address, so requests for http://www.megahotserved.com/ are hitting the very same vhost and getting into a circular redirect. So the redirect is working fine; you just have a different part of the server config to fix.
Agree with the above, and a small addition: it is better to redirect non-www to www rather than rewrite, otherwise you have two complete views ("copies") of your entire website; each page has two URLs, instead of one canonical one. This can be bad for search engines and other things.

RewriteRule for mapping x.domain.com to y.domain.com

Is it possible to redirect all requests to x.domain.com/.* to y.domain.com/.* WITHOUT letting this redirection be visible in the url?
I have unsuccessfully tried several things in .htaccess. Just specifying the [L] flag still shows this redirection in the url (as it does when I use the [R] flag additionally).
EDIT: as somebody claimed there being no reason for this, let me give some more information :)
I have one nice url: x.domain.com , which is well known.
Then there are a number of other domains: spring.domain.com , summer.domain.com , autumn.domain.com, winter.domain.com .
Depending on the time of the year, a specific y.domain.com becomes the current one. The x.domain.com should always map to the current one.
EDIT2:
I'll write here, as the code isn't nicely rendered in the comments...
I tried what Arjan suggested:
RewriteCond %{HTTP_HOST} ^x.domain.com$
RewriteRule ^(.*)$ /path/to/y.domain.folder/$1
Unfortunatly though this keeps redirecting forever. :(
Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
Putting the [R] flag behind, I see in the url something like:
http://x.domain.com/path/to/y.domain.folder/path/to/y.domain.folder/path/to/y.domain.folder/ ...
Any suggestions?
Now that I can read the errorlogs, I can give a direct response, as what a possible 500 error refers to.
Assuming you have access to the Apache configuration, create the following virtual host for domain x.domain.com. Then simply update y to whatever you need each season.
<VirtualHost ...:80>
ServerName x.domain.com
UseCanonicalName Off
ProxyRequests Off
<Proxy *>
Order Allow,Deny
Allow from all
</Proxy>
ProxyPreserveHost Off
RewriteEngine On
RewriteRule ^$ http://y.domain.com/ [P,NC]
RewriteRule ^/(.*)$ http://y.domain.com/$1 [P,NC]
ProxyPassReverse / http://y.domain.com/
</VirtualHost>
Also to pick up the Alias suggestions, if you have multiple virtual hosts (one for each season) then you could put a server alias into the current domain. E.g.
<VirtualHost ...:80>
ServerName summer.domain.com
ServerAlias x.domain.com
...
</VirtualHost>
<VirtualHost ...:80>
ServerName spring.domain.com
...
</VirtualHost>
...
This would make Apache deliver the summer.domain.com pages if you go to x.domain.com. If your seasonal subdomains depend on the HOST header line to be set correctly (i.e. to season.domain.com) you would need to use the first suggestion above, though.
If these are not hosted on the same server, then you'd need the Proxy flag. This also requires the proxy module to be running. Not tested:
RewriteCond %{HTTP_HOST} ^x.domain.com$
RewriteRule ^(.*)$ http://y.domain.com/$1 [P]
EDIT: Given the edits to your question they're probably just on the same server. So then indeed, as jetru suggested an Alias might do. Or:
# No RewriteCond required; serve all content from other folder:
RewriteRule ^(.*)$ /path/to/y.domain.folder/$1
EDIT: The above would not change the HTTP_HOST header that was sent by the browser (maybe that can be done as well). This implies that it would only work if the subdomains are represented on the file system as separate directories. So, as the .htaccess would then be placed in the directory holding the website for x.domain.com, the RewriteCond wouldn't even be required. Also, the directory for this x.domain.com subdomain would in fact not need any HTML content then; in the end all content would be served from the directory of another subdomain.
EDIT: As the above does not seem to work either, and yields endless rewrite loops even when adding [NS], maybe simply adding [L] helps here:
RewriteRule ^(.*)$ /path/to/y.domain.folder/$1 [NS,L]
Or maybe one can set an environment variable to stop the loop:
RewriteCond %{ENV:MY_VAR} !=1
RewriteRule ^(.*)$ /path/to/y.domain.folder/$1 [E=MY_VAR:1]
But, for both [L] and [E]: I'm just guessing; I've never made mod_rewrite jump into the directory of another virtual host. I am not sure it can be done to start with.
Unfortunately, it's unclear how one would add a new subdomain. If one would just need to create a new directory with the name of the subdomain (without any use of some administrative tool) then the provider might be be using system wide rewriting as well. In fact, even without subdomains the provider might be doing some Mass Virtual Hosting as described in the URL Rewrite Guide.
I guess the best solution would be to change the value of HTTP_HOST on the fly, to solve issues with any system wide rewriting. Maybe the following is allowed to achieve that:
RewriteCond %{HTTP_HOST} ^x.domain.com$
RewriteRule ^(.*)$ /path/to/y.domain.folder/$1 [E=HTTP_HOST:y.domain.com]
Again, as the above would only be present in the .htaccess in the x.domain.folder, the RewriteCond is probably not needed at all.
Have you tried
Alias /dir/file.html /full/path/to/other/file.html
??
To my knowledge and testing with firebug a redirect via .htaccess is always announced to the client and it's up to him how to proceed. It is therefore not an alternative to some sort of SSI functionality. To prevent a "fake" address modern browser should always make the REAL address visible to the user, however I think I have seen some misbehavior in programs like "feeddemon" where IE is embedded. If you - for whatever reason - really want to show content from one subdomain on another you can try using Javascript or (i)frames on the user side or some include functionality on the server site (eg. file_get_contents with php). However, I don't recommend this.