The whole URL of my system is changing, as it is moved to a different location. I'd like to use my old Apache to forward 301 all calls to the new system for some time.
I have deeplinks like this:
https://old.example/groups/something
which will stay the same on the new system - but with a different base-url, so like:
https://new.example/groups/something
I'm looking for an Apache rewrite rule that sustains everything after old.example/ while changing the base-url part and sending the user over to the deeplink location he asked for in the new system.
I tried:
RewriteCond %{HTTP_HOST} old\.example$
RewriteCond %{REQUEST_URI} ^\/$
RewriteRule .* https://new.example/ [R=301,L]
but this only works if the base URL is called directly, not for deeplinks.
RewriteCond %{HTTP_HOST} old\.example$
RewriteCond %{REQUEST_URI} ^\/$
RewriteRule .* https://new.example/ [R=301,L]
Your second condition (RewriteCond directive) is specifically checking that the REQUEST_URI is the document root ("base URL") only. You are also not passing the requested URL-path to the target URL.
However, if the new site has moved to a "different location" and the new and old domains point to different servers then you can use a simple Redirect directive on your "old Apache" server to redirect everything and maintain the same URL structure. For example:
Redirect 301 / https://my.new.system.url.net/
The Redirect directive is prefix-matching and everything after the match is copied onto the end of the target URL. So, a request for /groups/something is redirected to https://my.new.system.url.net/groups/something.
Test with a 302 (temporary) redirect to avoid potential caching issues.
Reference:
https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect
Aside: Just a quick note on the "unique" terminology you have used to describe the URL, as it's a bit confusing...
What you call the "base-url" is really the hostname (or domain name).
And what you call a "deeplink" is really the URL-path (or the remainder of the URL). The URL-path starts with the first slash after the hostname.
Related
I'm currently working on an Apache 301 redirect from one domain to another:
https://www.foo.com -----> https://www.bar.com
A question I have is: does a solution exist where I can redirect the first domain to the second domain, but have the URL still display the original url?
My current rewrite rule is as follows and works perfectly for the intended solution, but the URL does change:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?foo\.com$ [NC]
RewriteRule (.*) https://www.bar.com [R=301,L,NC]
Is there a modification I can make to achieve this functionality? A question was raise and I'm not quite sure if this is possible, as I have never handled something like this in the past. Thanks in advance for your help.
For some reason search engines are indexing my addon domains on my hosting. They should not do that.
For example I just found urls like
addondomain/maindomain.com
how to prevent this happening? How did search engines even find my addondomains?
What is the solution here? I tried this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^addondomain\.maindomain\.com
RewriteRule ^(.*)$ http://www\.maindomain\.com [L]
but when I visit the url for example
addondomain/maindomain.com for example nothing happens?
Try to Change in .httaccess file. You can replace your rule with this rule::
RewriteCond %{HTTP_HOST} =shop.domain.abc
RewriteRule ^ http://www.domain.abc/? [R=301,L]
Using %{REQUEST_URI} will cause original URI to be copied in target. Trailing ? in target will strip off any pre-existing query string.
This answer is a further explanation to my comment on your question.
how to prevent this happening? How did search engines even find my
addondomains?
Google can easily find these subdomains on your site. To prevent this from happening, you can set a redirection with a 301 status code to inform Google that it should not index the addon domain. By doing this, Google will update its index as well.
This is a very common scenario with shared hosting and specially when you use CPanel. In Hostgator's support pages, you can see they have mentioned about this behavior.
Addon URL Example
For the primary domain abc.com, if you assign the addon domain 123.com
to the folder "123," the following URLs would be correct:
abc.com/123
123.abc.com
123.com
All three of these paths would access the same directory and show the
same website. For visitors going to 123.com, there is no evidence that
they are being routed through 123.abc.com.
https://support.hostgator.com/articles/cpanel/what-is-an-addon-domain
You can fix this by adding the following to your .httaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^addondomain\.maindomain\.com$ [NC]
RewriteRule ^ http://www.maindomain.com [R=301,L]
NC - match in a case-insensitive manner.
R - causes a HTTP redirect to be issued to the browser. When given as
R=301 it will be issued with a 301 status code which is required to
inform Google that their index should be updated accordingly.
L - Causes mod_rewrite to stop processing the rule set. In most
contexts, this means that if the rule matches, no further rules will
be processed.
===========================================================
Edit: Updated to add redirection to all domains, as requested in the comments.
To do this, you can simply check if the hostname is equal to your main domain, and if it's not, redirect it to the main domain.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com$ [NC]
RewriteRule ^ http://www.maindomain.com [R=301,L]
Hope it helps :)
I'm making a JavaScript web app running on an Apache 2 server. I'm wondering if it's possible (either with mod_rewrite or some other mod) to make any path you type load the index.html from the root path, but keeping the URL?
For example: "example.com/blah/blegh" will load "example.com/index.html", but the address bar will still have "example.com/blah/blegh". Same if you tried typing "example.com/everything/is/index" would still load "example.com/index.html" and have "example.com/everything/is/index" in the address bar.
A simple answer about any mods I would need to use and which commands might be best would suffice. Though a code example would be very useful since I'm new to regex's and Apache rewriting.
Thank you for your time :)
Note: I'm doing this since I'm using History.js to parse URLs/titles into the address bar and tab titles while navigating (a one-page dynamic site). I'd like to be able to just load up the root index.html with the user's initial URL request and respond to users' actions that way much like a REST server.
Actually, you want to rewrite without redirecting. This requires enabling mod_proxy and mod_rewrite in Apache's httpd.conf.
Then, the rewrite should look like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.html [NC,L,QSA]
Reference:
What exactly does the Multiviews options in .htaccess?
htaccess rewrite without redirect
Apache: RewriteRule Flags
I'm preparing for migration from WordPress to site written via Codeigniter. I use htaccess very seldom, and never redirection.
My site is working now and clients visit my site. And I cannot just upload new site.
So I tried to practise with one page for redirection to another created for testing. I tried with encoded and decoded URL but without success; however, as written in the manual, it should be a simple:
Redirect [status] URL-path URL
.htaccess:
RewriteEngine On
Redirect 301 /?wpsc-product=подвеска-сова-медь-duplicate http://domain.com/?page_id=851
Also, Apache has RedirectMatch and RewriteRule [301] and they are loading server, so I prefer to use simple redirects (I have CPU load limitation on my hosting).
I have about 500 links.
Redirect OR RedirectMatch directive from mod_alias cannot match query string. You must use mod_rewrite like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} wpsc-product=подвеска-сова-медь-duplicate [NC]
RewriteRule ^/?$ http://domain.com/?page_id=851 [L,R=301,B]
Make sure to keep this rules on top of your .htaccess.
Reference: Apache mod_rewrite Introduction
So I'm building a new site and everything at the old site is going to be scrapped - including the domain structure, and domain name. I currently have a redirect rule in my .htaccess setup to redirect http://jagdesignideas.com to http://jag.is as such:
RewriteCond %{HTTP_HOST} ^(www.)?jagdesignideas.com
RewriteRule ^(/)?$ "http://jag.is" [R=301,L]
But the trouble is if a user follows a link from elsewhere on the intarwebz to http://jagdesignideas.com/about, for example, that does not redirect to http://jag.is as I would like. Instead it brings up the 404 page from the jagdesignideas.com domain.
How to I get all substrings of jagdesignideas.com domain to endup at the new redirect?
EDIT: To be clear, I'm not trying to match pages on the old site to the new site, only do a mass redirect of jagdesignideas.com and anything there just to point at the jag.is domain.
The isse us what you are matching in your RewriteRule: ^(/)?$ matches the root slash or nothing, which means you are only rewriting calls to your domain root. You need to capture all path components instead, using ^.*$, which matches anything after the domain root, or nothing. This will redirect any call to your old domain, with or without a path, to the root of your new domain:
RewriteCond %{HTTP_HOST} ^(www\.)?jagdesignideas\.com [NC]
RewriteRule ^.*$ http://jag.is [R=301,L]
If you want to redirect your path structure to an identical one on your new server, this will do the trick:
RewriteRule ^.*$ http://jag.is/$0 [R=301,L]
Starting from there, you can basically do any kind of redirect to map old to new path structures – see the Apache Module mod_rewrite reference.