writing htaccess rules for redirection - php

I have a website, lets say www.domain.com
and my current blog is at www.domain.com/blog/
so my URLs are like www.domain.com/blog/recent-post/
now Im having a new wordpress installation to handle blog, and pointed a subdomian to it, so the blog now is blog.domain.com
I have lot of seo work dne forwww.domain.com/blog/ I dont want to miss that, and I want to redirect users visiting www.domain.com/blog/ to www.blog.domain.com/
and www.domain.com/blog/any-url/ to www.blog.domain.com/any-url/
can some one help in writing redirection rule for this?

If in spite of #Cailbuig's advice you still want to do this, what you'd need is
Options +FollowSymLinks -MultiViews
RewriteRule ^blog(/.*)?^ http://blog.domain.com$1 [L,R=301]
Start with R=302 and then when it works change to R=301. 301 reponses are cached by browsers, so they are hard to test.

Why do you want to change domain.com/blog to blog.domain.com?
This isn't good for SEO, since the links do not have the same value for a directory that for a subdomain.
Better to have the blog in a folder that subdomain, since the authority will always go to main domain (domain.com).

Related

Rewriting URLs through htaccess with multiple versions of the same page

So I did quite a bit of research, but since I'm very new to manipulating .htaccess, I couldn't really find a good answer.
This is my site structure:
www.kanadax.ca/index.php is the landing page. This is what Guest users (in Joomla speak) see when they land on the page, before they signup.
www.kanadax.ca/index.php/anasayfa-2 is the home page for group 1 users, aka non-paying registrants.
www.kanadax.ca/index.php/anasayfa-3 is the home page for group 2 users, aka paying registrants.
Now ideally, I would like to make all of these just be www.kanadax.ca/ in the URL bar. The server should know which one to send the user to, but the end user should just see www.kanadax.ca/. I'm not quite sure if this is possible, so this is my first question. If so, what should the .htaccess look like?
If this isn't possible, can I at least take out index.php from each? So they would be
www.kanadax.ca/
www.kanadax.ca/anasayfa-2
www.kanadax.ca/anasayfa-3
Now of course, anasayfa-2 and -3 are not good practice (anasayfa=home) so I would be very thankful if someone can walk me through removing those if it's not possible to simply make all of these be displayed as www.kanadax.ca/
I hope I was able to explain things clearly but please don't hesitate to ask questions.
This is usually solved by enabling Use URL Rewriting and Search Engine Friendly URLs within the Global Configuration settings for Joomla. Be sure you have a Joomla .htaccess file aswell. There should be a provided htaccess.txt file you can copy to .htaccess if you don't. Additionally, confirm your Joomla cache has been cleared and your browser cache has been cleared before attempting to check for changes.
you can redirect your index.php page to your domain
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ http://www.kanadax.ca/$1 [R=301,L]
althogh you cannot redirect user to a specific page because info are stored in joomla session and you cannot make any rule or condition in htmlaccess basing on that.

Keep old website (HTML files) on webserver but disallow search agents to index them

I’ve just finished a website for a client who is going to replace their old (very old, HTML hard-coded website). The problem is that they (for now) want to save their old website and all the files on the webserver in the original position. This does not create any issues with the new website which is made in PHP and Wordpress but it makes a big deal when Google (and others) are dropping by with their search robots and indexing.
When doing a Google search it still finds the old HTML files. Is there any way that I could “keep” the old HTML files on the web server but make sure that for the first no robots are going to index them and if anyone is trying to navigate to an HTML page, e.g. http://www.clientdomain.com/old_index_file.html, they are getting redirect? I think the last part might be able to be done in .htaccess but I haven’t found anything useful searching for it.
The first question about not allowing robots and agents to index HTML files, I’ve tried to put these two lines in my robots.txt file
Disallow: /*.html$
Disallow: /*.htm$
But I’m unsure if it will work?
I might deal with this in a completely wrong way but I’ve never tried that a client has requested to keep the old website on same server and in original location before.
Thanks,
- Mestika
<?php
$redirectlink = ‘http://www.puttheredirectedwebpageurlhere.com‘;
//do not edit below here
header (‘HTTP/1.1 301 Moved Permanently’);
header(‘Location: ‘.$redirectlink);
exit;
?>
This code will use a 301 redirect the page to the URL that you desire. The filename of this .php should be the URL slug of the page you want to redirect.
301 Redirect
A 301 redirect, or also known as a permanent redirect, should be put in place to permanently redirect a page. The word ‘permanent’ is there to imply that ALL qualities of the redirected page will be passed on to the detour page.
That includes:
PageRank
MozRank
Page Authority
Traffic Value
A 301 redirect is implemented if the change you want to make is, well… permanent. The detour page now embodies the redirected page as if it was the former. A complete takeover.
The old page will be removed from Google’s index and the new one will replace it.
Or you can do it in your htaccess like shown by the above poster.
There's probably a lot of ways to handle this, assuming you have a clear mapping of pages from the old template to the new one, you could detect the Google bot in your old template (see [1]) and do a 301 redirect (see [2] for example) to the new template.
List item
[1] how to detect search engine bots with php?
List item
[2] How to implement 303 redirect?
Will take some work, but sounds like you'll need to crack open your htaccess file and start adding 301 redirects from the old content to the new.
RewriteCond %{REQUEST_URI} ^/oldpage.html
RewriteRule . http://www.domainname.com/pathto/newcontentinwp/ [R=301,L]
Rinse and repeat
This is definitely something mod_rewrite can help with. Converting your posted robots.txt to a simple rewrite:
RewriteEngine on
RewriteRule /.*\.html /index\.php [R]
The [R] flag signifies an explicit redirect. I would recommend seeing http://httpd.apache.org/docs/2.4/rewrite/remapping.html for more information. You can also forbid direct access with the [F] flag.

What is the best way to do a 301 redirect for A LOT of pages individually?

Any suggestions, I have about 300 pages which need to be setup with a 301 redirect to my new domain. Can anyone shed light on how to do this programatically, I really hope it does not have to be done individually!!!
I am running an Apache server on CentOS 5.4
old page -->
http://example.com/examplePage.php?id=289
new page -->
http://gallery.example.com/en-US/theme/6
This issue is that the old domain is not related to the new in anyway. 2 separate databases with complete different ID values. It is a complete overhaul of the site in which the id values were not keep in sync. I think this is where my problem becomes a manual entry for each individual redirect I need to keep the SEO values intact.
If anyone could suggest how to do it programatically in PHP that would be great, otherwise I fear I will have hundreds of entries in my .htaccess file.
Try using url rewrites to redirect all pages to the same 301 page. Url rewrites works differently on each server. For Apache you have mod_rewrite.
From your question it's hard to give you a specific answer, but I've encountered your situation dozens of times before and here is what I would recommend:
As Dani said use Apache mod_rewrite to handle the redirects if you can by placing them in an .htaccess file at the root of your site.
If you are simply changing the domain, but all of the page urls are staying the same then you only need one line in your .htaccess file:
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
However if your page urls are changing you can use Apache to re-direct them individually. A typical 301 redirect in Apache looks like this:
redirect 301 /oldpage.htm /newpage.htm
If for some reason you're unable to use Apache to handle the re-direction you can do it using PHP like this:
$redirects = array(
'/old-page' => '/new-page',
'/old-page-two.htm' => '/new-page-two'
);
if (array_key_exists($_SERVER["REQUEST_URI"],$redirects)) {
header("Status: 301 Moved Permanently");
header("Location:http://".$_SERVER["HTTP_HOST"].$redirects[$_SERVER["REQUEST_URI"]]);
exit;
}
The only way to reduce the effort of creating the re-directs is if there are common patterns in the urls you are trying to re-direct e.g.
/product/123 /product-123.html
/product/456 /product-456.html
/product/789 /product-789.html
If this is the case then you can use regular expressions to match the patterns like this:
RewriteRule ^product/([0-9]*)$ /product-$1.html [R=301,L]
If you tell us more about your situation and give a few example urls we may be able to help further.

.htaccess & wordpress site - How to add 301 redirect rules for no longer valid permalink?

I recently did a big update on my wordpress site, thus some old permalink are invlid now.
Seach google for a tutorial on this and found a lot about static html page redirection examples or specific php page which are not what i'm looking for
Both my old and new permalinks are in path format, for example, i need redirect a couple urls of old gallery posts (deleted) which are
/2009/06/gallery/abc/
/2009/06/gallery/cba/
/2009/06/gallery/bbc/
/2009/06/gallery/aab/
to a new page which is
/gallery/
How do i write a correct redirect rule for this?
RewriteRule ^[0-9]{4}/[0-9]{2}/gallery/.*$ gallery [R=permanent,L]
Should get you to where you need to be. If you need the actual parameters (IE Date / Month / Title) passed along it is a bit of a change up, but do-able.
EDIT
Fixed typo.
Try something like this:
RewriteEngine On
RewriteRule ^/2009/06/gallery/\(?([^/]*)\/$ /gallery/ [R=301,L]
Personally, I would use the Redirection plugin rather than editing your .htaccess directly -- it's easier, less danger-prone, and will let you log what redirections have been happening. It will also track 404 errors so you can see if you've forgotten to redirect anything.
Then, if you're just redirecting a couple of fixed posts, it's just a matter of adding a simple rule for each redirect, with the old and the new URLs, on the Redirection configuration page. You can also use regular expressions, as with .htaccess, if you want to do anything more complicated.

Changing WordPress URL structure while maintaing the proper 301 redirects with mod_rewrite

I currently have a blog set up with WordPress using URLs like so:
www.domain.com/blog/?pid=384092817
This was before I knew anything about anything. There's a significant amount of content there now and I want to change my URL structure to:
www.domain.com/my-post-title-384092817
Is there a way to set up my .htaccess and mod rewrite so when users go to the first url they get a 301 redirect to the second URL? I know to change my WordPress address (URL) setting from www.domain.com/blog to www.domain.com and my permalink setting to /%postname%-%post_id%, but how do I tell the old URLs to redirect to the new ones?
Do you actually mean that when users go to the second URL, it will be rewritten to the first? That can be done with
RewriteRule /blog/.+-(\d+)$ /blog/?pid=$1
If you want to send 301 redirects from the old URLs to the new ones, then you can't really do that with an .htaccess file. The reason is that Apache's mod_rewrite doesn't have access to the title of your post, so it won't know what title to insert in the URL. You'd have to do that redirect with PHP.
EDIT: y'know what, actually that's not entirely true. You can do some pretty crazy things with the RewriteMap directive, such as instructing Apache to ask an arbitrary program to perform the rewriting, and in that way you could have, say, a PHP script that does some database work to figure out what the title is and rewrites the URL appropriately based on that. But that seems like a way overcomplicated solution and I wouldn't suggest doing it - it'll be much cleaner and easier to just send the redirect directly from PHP.
Depending on your WP version, you can just use the Permalink redirect plugin -- should do the trick for you within WordPress and without mod_rewrite. However, as of WordPress 2.3, a lot of this should work automatically. At that point, the only thing you should have to do is redirect all your /blog/... requests to the route, which you can do via mod_rewrite roughly like this:
RewriteRule ^blog(.*) $1 [QSA]

Categories