Changing the name of a domain internally - dns

I have given my client a domain like www.xyz.com/test.
Now for some reason I want to change it to www.xyz.com/testing but I want that client will continue to type www.xyz.com/test.
How can I do it?

You can setup a HTTP 301/302 redirect from /test to /testing.

Well, if you're on an Apache server you should be able to use an htaccess file to route the URL to the actual location on your server. There's a couple of ways of creating 301/302 redirects, such as a plain RewriteRule that routes all calls to "xyz.com/testing/..." to the actual location on the server, "xyz.com/test/...".
Good luck! =)

Related

Redirect visitor to the same url that he/she write without database

i have 2 websites let says (example1.com and example2.com).
if the visitor write in the url any number for example (example1.com/123456) i want it to re-directed to (example2.com/123456) so the domain only replaced not the numbers the visitor written.
i need that happen using htaccess file or any simple methods without database or save any data because i have only HTML pages, and its hard to me to do it because i am still learning.
All you need is a redirection on protocol level. That is possible with all usual http server's, the exact solution depends on which http server you are using to serve the domain "example1.com".
Since you tagged your question .htaccess and mod-rewrite I assume that you are using the apache http server. If so you can implement a rule like that one:
RewriteEngine on
RewriteRule ^/?(\d+)$ https://example2.com%{REQUEST_URI} [R=301,L]
It will redirect all requests to example1.com that use a path that consists only of digits to the second domain while preserving the requested path. An external redirection will get performed using a http status 301 ("moved permanently") as a response to the first request to example1.com.
If both domains are served by the same http server and you do not have setup separate virtual hosts you probably have to add a condition to that to make sure the rule only gets applied to requests to the first domain:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www.)?\.example1\.com$
RewriteRule ^/?(\d+)$ https://example2.com%{REQUEST_URI} [R=301,L]
Preferably such a general redirection rule should get implemented in the central http server's host configuration responsible for serving example1.com. If you do not have access to that you can indeed use a distributed configuration file instead, often called ".htaccess". If so you need to enable that feature beforehand using the AllowOverride directive. The configuration file needs to be located in the hosts top level DOCUMENT_ROOT folder and it needs to be readable for the http server process.

Can I redirect a user to a Google Drive PDF while keeping my URL in the address bar?

If I have a user follow a link to my site such as
mydomain.com/pdf/google_token
is there a way for me to redirect them to the Google pdf
drive.google.com/file/d/google_token/view
while keeping
mydomain.com/pdf/google_token
in the address bar?
Right now I am redirecting to google successfully using
RewriteRule ^pdf/([a-zA-Z0-9]+)$ https://drive.google.com/file/d/$1/view
in my .htaccess file, but it is replacing the URL with
drive.google.com/file/d/google_token/view
Thanks.
You are not looking for a way to redirect. A redirection always changes the URL in the client, that is the whole purpose of a redirection. What you are looking for is a proxy solution, maybe in conbination with an internal rewrite. That creates a kind of mapping: the content published on that google resource is re-published through your http host.
This would be an example for such setup:
ProxyPass /google-drive/ https://drive.google.com/
ProxyPassReverse /google-drive/ https://drive.google.com/
RewriteEngine on
RewriteRule ^/?pdf/([a-zA-Z0-9]+)$ /google-drive/file/d/$1/view [END]
An alternative would be to only re-publish a section of that remote resource:
ProxyPass /google-drive/ https://drive.google.com/file/d/
ProxyPassReverse /google-drive/ https://drive.google.com/file/d/
RewriteEngine on
RewriteRule ^/?pdf/([a-zA-Z0-9]+)$ /google-drive/$1/view [END]
That set will work likewise in the http servers host configuration and you probably can also get it to work using a dynamic configuration file (".htaccess" style file). If you really need to use such a file then take care that its interpretation is enabled in the host configiration. And you definitely need to have apache's proxy module loaded. You should prefer to place such rule in the http servers host configuration though, for various reasons.
If that setup is not possible, for example because you do not have access to the proxy module, then you can implement a simple routing solution which fetches the PDF in background using for example php's cURL extension and forwarding the payload along with correct http headers to the client that sent a request to that PDF. That is usually done for resources kept locally but there is no reason why you can't do that with remote resources too.
Some additional notes:
if you only deliver documents form that google drive resource, then you probably do not need the ProxyPassReverse directive, but only the ProxyPass.
if you run into a server internal error (http status 500) using the above setup then chances are that you operate a very old version of the apache http server. You will find a hint on an unsupported END flag in your http servers error log file in that case. Try using the [L] flag in that case, it probably will work the same here, but that depends on the rest of your setup.

If mod_proxy is unavailable, what is the effect of not using the P flag in an htaccess rewrite?

For a mobile subdomain (m.domain) I'm simply trying to pull images from the common main domain image folder (www.domain.com/images)
After a bit of tinkering, I've apparently gotten the htaccess rewrite code to work successfully but I found out I was unable to use the P flag on rewrite, and that I don't have mod_proxy enabled due to shared hosting.
simple code in my m.domain.com htaccess file:
RewriteRule ^(images/.*) http://www.domain.com/$1 [L]
What benefit am I not receiving by not using the P flag? What's the difference?
When you proxy a request, the server itself does the external request and passes through the data it receives. If the client would request http://m.example.com/asdf, and the server would proxy to http://www.example.com/asdf, the server would make a proxy-request to that url, then return that data for http://m.example.com/asdf to the client. For the outside world there is duplicate content on http://m.example.com/asdf and http://www.example.com/asdf, and any client requesting both urls will cache things twice. This is not ideal.
With your current approach you make an implicit temporary redirect. When the client requests http://m.example.com/asdf, the server sends back a redirect-response, which tells the client to try again on http://www.example.com/asdf. The client tries again on that url, and receives the data from the server. For the outside world it is clear that the content is in one place, and when caching the data it will only cache it for http://www.example.com/asdf. If you make the redirect permanent, it will skip the redirect in some cases and directly request http://www.example.com/asdf, reducing the load on your server.
Unless you have a good reason to proxy a request (e.g. because the server with the data is only available locally), you should not use a proxy request. If http://m.example.com/ and http://www.example.com are hosted on the same server, and you are unconcerned about hosting duplicate content, then you should use an internal rewrite. If you are concerned about duplicate content, you use an external redirect, using the [R] flag.

1 domain.. 2 server and 2 applications

i have a site like twitter.com on server one and on server two i have forum, which path is like domain.com/forum
on server one i wanted to implement wild card dns and put main domain on it. but on server two i wanted to keep forum separate, i cant give sub-domain forum.domain.com, because all its links are already put in search engines and link back to domain.com/forum.
so i was wondering, how can i put domain and wild card dns on server one and still able to give path on server 2 for domain.com/forum (as sub-folder).
any ideas?
do you think htaccess can do that job? if yes, then how?
You could use htaccess and mod_rewrite so domain.com/forum actually displays pages from forum.domain.com.
Maybe something like this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule domain.com/forum/(.+) forum.domain.com/$1
Easy - use a proxy! If you like apache you will love apache mod_proxy for your purpose.
<VirtualHost *:80>
ServerName maindomain.com
ServerAlias *.maindomain.com
# insert document root and general settings for this domain here
# ...
ProxyPass /forum http://forumdomain.com
ProxyPassReverse /forum http://forumdomain.com
ProxyPassReverseCookieDomain forumdomain.com maindomain.com
</VirtualHost>
This configuration makes apache do a HTTP-request to your internal domain (forumdomain.com) without notifiying the users browser of the internal location. Your Forum will be accessable at http://*.yourdomain.com/forum. Cookies and headers the forum sents will get rewritten accordingly and Search-engines will not take notice of your backend-server.
You can read more about it at http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
Should you need to rewrite reference sin your html (href, src ...) you might google on "mod_proxy_html".
A solution like this could of course be build with other intelligent proxyservers like squid as well. You can use it to map any content from "backend servers" to your public domain.
Make sure routing is OK or set up a host-entry for your internal domain (forumdomain) with a internet ip-addresse 192.168 ...
Enjoy your site and give feedback how worked out :)
p.s.: a "RewriteRule" directive can potentially do the same thing for you but the rdirect will be visible (and executed) by the client unless you specify the "P", foricng it to do an internal proxy request. If available I would prefer the mod_proxy though as it is more versatile and allows for more configuration.
If you use a proxy on server 1 pointing to server2, you will increase the load on server 1, since all traffic will be routed through it. Besides, if server 1 goes down, nobody will be able to reach server 2 either. Of course it is possible though, but these things are to be considered.
I would suggest setting up a supdomain for server 2 anyway, like forum.domain.com, and on server 1 you set up a 301 redirect from domain.com/forum to forum.domain.com using mod_rewrite from htaccess. Using that technique, you can even redirect calls to specific links to their corresponding page on server 2. Search engines will follow the 301 and they will eventually update the index.
You can use a 301 redirect to make sure the search engine update their index with your new urls like so:
RewriteRule domain.com/forum/(.*) http://forum.domain.com/$1 [R=301,L]
If you've got two servers you don't really have much choice but to use a redirect (ideally a 301 Permanent redirect) to move users from domain.com/forum to forum.domain.com.
The only other way to do it would be to put a reverse proxy in front of those two servers, which reads the URL and internally directs the query to the right server, but that's then an extra piece of hardware.

What's a clean/simple way to ensure the security of a page?

Supposing you have a form that collects and submits sensitive information and you want to ensure it is never accessed via insecure (non-HTTPS) means, how might you best go about enforcing that policy?
If you're running Apache, you can put a RewriteRule in your .htaccess, like so:
RewriteCond %{HTTPS} "off"
RewriteRule /mypage.html https://example.com/mypage.html
I think the most bullet-proof solution is to keep the code inside your SSL document root only. This will ensure that you (or another developer in the future) can't accidentally link to a non-secure version of the form. If you have the form on both HTTP and HTTPS, you might not even notice if the wrong one gets used inadvertently.
If this isn't doable, then I would take at least two precautions. Do the Apache URL rewriting, and have a check in your code to make sure the session is encrypted - check the HTTP headers.
Take a look at this: http://www.dotnetmonster.com/Uwe/Forum.aspx/asp-net/75369/Enforcing-https
Edit: This shows solutions from an IIS point of view, but you should be able to configure about any web server for this.
In IIS? Go to security settings and hit "Require secure connection". Alternately, you can check the server variables in page load and redirect to the secure page.
I'd suggest looking at the request in the code that renders the form, and if it is not using SSL, issue a redirect to the https URL.
You could also use a rewite rule in Apache to redirect the user.
Or, you could just not serve up the page via HTTP, and keep it only in the document root of your HTTPS site.

Resources