How do you implement simple website redirects? - http

If you go to www.codinghorror.com, it is automatically redirected to www.codinghorror.com/blog
How does one re-direct to the "/blog/"?
I'm using Dreamhost Shared Hosting, so my options of configuring the server are limited.

This would be a good opportunity to teach yourself about HTTP status codes. Read the status code definitions section of the HTTP 1.1 spec; section 10.3, "Redirection 3xx," covers the status codes pertinent to your question. Knowing the HTTP 3xx codes means that you can send a response that has exactly the semantics that you intend. For example, if you intend for the redirect to always occur, you could use HTTP 301, "Moved Permanently." In theory, a client could use the HTTP 301 response as a signal to store the new URL value in place of the old one.
Your configuration options at Dreamhost are not as limited as you would think, because you can specify many Apache web server configuration directives in a hidden .htaccess file. This file should be placed in a web document directory; using your example, you would place it in the root web document directory, /home/midas/codinghorror/, though .htaccess files can be placed in any directory served by Apache. (Remember to include the leading dot in the filename.) Its contents would be either the mod_alias example or the mod_rewrite example already mentioned.
Note that mod_alias's RedirectPermanent directive will send an HTTP 301 status code for you. If you wanted to use mod_rewrite to do this, you could specify the status code:
RewriteEngine on
RewriteRule ^$ /blog/ [R=301]
If you use [R] without a code, then HTTP 302 ("Moved Temporarily") is used.
Since PHP is also available to you, that's also an option, though it's possible that the above options using .htaccess are faster. You would place a file called index.php in /home/midas/codinghorror/ and use the code given by Jeremy Ruten above. Again, you can specify a status code in the third argument to header():
<?php
header('Location: http://www.codinghorror.com/blog', TRUE, 301);
?>
Otherwise, using PHP's header() function with 'Location' defaults to sending an HTTP 302 status response.

Another option is using Apache's mod_alias to do a permanent redirect (in either .htaccess or httpd.conf):
RedirectPermanent / /blog
There are probably as many ways as there are servers and programming language (probably even more then that). If you tell us what specific technology you use it can probably help to give you a more specific answer.

Assuming Apache with mod_rewrite and context permissions allowing its use, you put this in virtual host configuration or a .htaccess:
RewriteEngine on
RewriteRule ^$ /blog/

One way is by sending a 'Location' header to the client. Here's a PHP example:
<?php
header('Location: http://www.codinghorror.com/blog');
?>

Related

Redirecting Dynamic URLs with Multiple IDs using .htaccess (Wordpress)

I've just moved my site onto Wordpress, which means the URL structure has now changed. I'd like to use a Rewrite Rule in the .htaccess file to redirect dynamic URLs with multiple IDs in one single statement, but am not having any success. An example is:
OLD URL: example.co.uk/seasons/season.php?ID=1819
NEW URL: example.co.uk/seasons/1819
The statements I have tried in .htaccess are:
RedirectMatch 301 ^/seasons/season.php?ID=(.*).htm$ example.co.uk/seasons/$1
and
RewriteCond %{QUERY_STRING} ^?ID=1$
RewriteRule ^/seasons/season.php$ example.co.uk/seasons/? [R=301,L]
In neither case, the redirect fires. Is there something about a Wordpress .htaccess file that I'm not considering, or is the error with the statements I'm attempting. I'm using .htaccess 301 redirects for http to https and non-www to www without issue.
This is the fixed version of the approach you chose, to make an external redirection:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)ID=(\d+)(?:&|$)
RewriteRule ^/?seasons/season\.php$ https://example.co.uk/seasons/$1 [R=301,QSD]
Since both, the old and new URLs use the same host name you can simplify that:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)ID=(\d+)(?:&|$)
RewriteRule ^/?seasons/season\.php$ /seasons/$1 [R=301,QSD]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
These rules will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Redirecting All Subdirectories to Single Location

I am using .htaccess to rewrite my subdirectories or additional URL information in links to a single location. I'm not quite sure how to phrase this, so I'll include examples below.
OLD URL NEW URL
oldsite.com/shop newsite.com/shop
oldsite.com/shop/something newsite.com/shop
oldsite.com/shop/category/product/123.html newsite.com/shop
oldsite.com/shop/landingpage newsite.com/shop
There are still a number of additional functions that oldsite is performing, so I only want the /shop (and anything after that) to redirect to the newsite.
If it would be easier to do this somewhere besides .htaccess that would be great too.
That can easily be done with mod_rewrite:
RewriteEngine On
RewriteRule ^shop https://newsite.com/shop [R=301,L]
This will look at the URL and, if it begins with "shop" (the leading / is ignored automatically), redirect to the new URL.
Note that it will also redirect /shopping. If that is not what you want, you'd have to modifiy the pattern.
It will use a 301 Moved Permanently HTTP status code, but you could change that to another 3XX code by modifying the R flag.

Can not log in to Drupal 7 admin after changing to HTTPS. Access Denied and cookie not set

I can no longer log into Drupal 7's admin area after adding SSL certificate and forcing HTTPS in url.
The website was previously served via HTTP protocol and worked perfectly fine. I would also like to mention that if I revert back to HTTP mode, the site will work as expected. However, I am trying to implement HTTPS.
Additionally, while logging in, I have noticed that the cookie response would be sent by the server but it would get deleted immediately leading to access denied message.
So far, after going through mixed response in the internet, I have played around with session.cookie, $base_url and $cookie_domain variables within settings.php file. I haven't found a solution yet.
Has anyone bumped into this problem? Your response will be highly appreciated.
In apache server
Go to .httaccess
Add # in front of RewriteRule ^ - [E=protossl] to disable http
It looks like this # RewriteRule ^ - [E=protossl]
This solves the problem
Ok so this is a fairly old post but here is what happened with my site.
We installed a new SSL certificate and somehow the .htaccess file ended up empty, and I do mean 100% blank.
This means easy fix, download latest drupal core and just replace the .htaccess file along with the core files, usually you want skip this to preserve all additional code added for html compression and such, but in this case that is redundant.
After the .htaccess was replaced I then added the compression and rerouting code like before and problem solved.
Hope this helps someone else out, I know I will always be checking the .htaccess files first if it happens again.
Disable in .httaccess
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com*
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

Any way to *rewrite* example.com/blog/ URL to blog.otherdomain.com?

I have a Wordpress blog hosted on one server:
http://blog.example2.com/
And another site on a separate server:
http://www.example.com
Is it possible to get the blog to be served at the following URL?:
http://www.example.com/blog/
If so, I'd love to know how. I messed around with mod-rewrite, but it looks like it will only redirect (not rewrite) to another URL, in this case.
For those interested: I realize I could install the blog on the same server, but I'd rather keep things decoupled for now.
Many Thanks
You'll need to use mod_proxy:
ProxyPass /blog http://blog.example2.com/
ProxyPassReverse /blog http://blog.example2.com/
This'll need to be in server/vhost config. Otherwise, you can use it along with mod_rewrite in an htaccess file (in your document root):
RewriteRule ^/?blog/(.*)$ http://blog.example2.com/$1 [L,P]
ProxyPassReverse /blog http://blog.example2.com/
If you're using cookies in your blog, you'll need to make sure to correct the paths/domains.

needed a suggestion in redirecting old URLs to a new URL

Earlier i have installed my code in the main domain itself. for instance,
www.abcd.com/xyz/page.html
there are about 300 pages indexed by google with the old URL.
Now i have removed that code from the main domain and installed the code in Sub-Domain. So it make all the URLs indexed by Google and all the backlinks are pointing to the old URL.
Now i need to point all the old invalid URL to new Valid URL when Users clicks on the old URL.
Please suggest how to handle this.
Thanks a lot...
If you are using Apache, I suggest that you use mod_rewrite as it allows to send the HTTP status 301 Moved Permanently, informing search engines that the contents of your website has been moved to a new location (rather than just deleted).
In a .htaccess file:
RewriteEngine on
RewriteRule (.*) http://newdomain.mysite.com/$1 [R=301,L]
The first line enables URL "rewriting", the fancy term for redirections of all kind. The second line is decomposed like this:
RewriteRule is the directive to match URLs and change the place they point to;
(.*) is a regular expression matching any requested file path (without an initial slash)
http://newdomain.mysite.com/$1 is the place where you want to send your visitors, and $1 is expanded to the previously matched path
[R=301,L] tells Apache to send the 301 Moved Permanently HTTP status code, and that it's the last RewriteRule that can match this request (it's useful only when you have multiple RewriteRules but it doesn't hurt to have it anyways).
The next time crawlers visit your site, they will notice the HTTP status and update their links to your new address. You should have it set up as soon as possible before Google thinks your whole site went 404.
If you are using Apache as your web server you can use mod_rewrite or mod_alias command. This is more a server job and not a programming job.
You can put the following in a .htaccess file or in the vhost container.
mod_rewrite
RewriteEngine On
RewriteRule ^xyz/page.html$ new-page.php [L,NC,R=301]
See: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
mod_alias
Redirect permanent xyz/page.html http://domain.com/new-page.php
See: http://httpd.apache.org/docs/current/mod/mod_alias.html#redirect
PHP
You can use a Location header to perform a redirection:
header('Location: http://domain.com/new-page.php', true, 301);
Java
public void HttpServletResponse.setHeader(String name, String value)
See: http://docstore.mik.ua/orelly/java-ent/servlet/ch05_06.htm
This should be handled by the server, preferably using an htacces redirect
PHP:
header('Location: new.url.com');

Resources