I'm having a strange issue with a permanent redirection in PHP.
Here's the code I'm using:
if ($_SERVER['HTTP_HOST'] != 'www.mydomain.ca')
{
header("HTTP/1.1 301 Moved Permanently");
$loc = "http://www.mydomain.ca".$_SERVER['SCRIPT_NAME'];
header("Location: ".$loc);
exit;
}
So, the home page, referenced either by www.myolddomain.ca or www.myolddomain.ca/index.php both work but every other page on the site fails to redirect. I've spent a couple of hours looking at this from all the angles I know and can't fathom it. Does anyone have any idea what the issue could be?
As a note, I've tried this without the 301 header too and get the same issue.
If you have a ftp-account to the server, you can create a .htaccess file and let it handle the requests. Just create a file named .htaccess in the root-folder of your site and post the code, changed to your desired pattern(s)
Options +FollowSymLinks
RewriteEngine on
RewriteCond {HTTP_HOST} ^yourdomain.com
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
If you want to do it fix via PHP, I would create a redirect.php and include it on every site you need it. Hard to tell if this is the best solution, it is a bit depending on your way of layouting and structuring.
Try it by htaccess, create .htaccess file and try something like this
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
** Apache Mod-Rewrite moduled should be enabled
Related
I'm trying to change the URL of my website to show only the ID but It seems to not work...
I don't know why.. other commands of RewriteRule work well..
Actually the .htaccess file looks like belove
RewriteEngine On
RewriteRule ^/?([0-9a-zA-Z-]+)/$ article.php?articleId=$1 [L]
I want that it works like this:
Old_URL(to modify):
mywebsite.it/article.php?articleId=15
I want something like this:mywebsite.it/article/15
But the URL remains the same actually: always display this: mywebsite.it/article.php?articleId=15
Thanks in advance to every help :)
An internal rewrite will never change the URL visible in the browser. You are probably looking for an external redirection. Or better the combination of both:
RewriteEngine On
# externally redirect old URL
RewriteCond %{QUERY_STRING} (?:^|&)articleId=(\d+)(?:&|$)
RewriteRule ^/?article/?$ /article/%1 [R=301,L]
# internally rewrite new URL
RewriteRule ^/?article/(\d+)/?$ /article.php?articleId=$1 [END]
Those rules are meant to be implemented on top level. Best in the actual http server's host configuration. If you do not have access to that then a distributed configuration file will work (".htaccess") when located in the host's DOCUMENT_ROOT, but support for that needs to be enabled.
It is a good idea to start out using a R=302 temporary redirection and only change that to a R=301 permanent redirection once you are happy with how things work. That prevents caching issues on the client side.
Im writing Redirect Rule and came up on a problem that could interest some of you.
The idea is to redirect old path voyage/grece/mykonos/ to new path voyage/cyclades/mykonos/
But whatever i tried failed and redirect me to strange url :/ ->
http://www.agencedevoyage.com/voyage/europe/cyclades/mykonos/?continent=europe&country=grece&town=mykonos&type=voyage
Does that mean the php traitement is done without refreshing the page or something like that ? How can I avoid this ?
Rules I tried :
ReWriteRule \/voyage\/europe\/grece\/mykonos\/ http://www.agencedevoyage.com/voyage/europe/cyclades/mykonos/ [R=301,L]
RedirectPermanent /voyage/europe/grece/mykonos/ http://www.agencedevoyage.com/voyage/europe/cyclades/mykonos/
Try this
RewriteRule ^/voyage/europe/grece/mykonos/$ http://www.agencedevoyage.com/voyage/europe/cyclades/mykonos/ [R=301,L]
Or
Redirect 301 /voyage/europe/grece/mykonos/ http://www.agencedevoyage.com/voyage/europe/cyclades/mykonos/
Make sure that the opening of the .htaccess file contains the 2 lines of code below which enables the Apache module to rewrite the URLS, then place your redirections below them
Options +FollowSymLinks
RewriteEngine On
I have an interesting situation. A site I was working on used to be in wordpress and there was a plugin called simple 301 redirects - what it did, was... create redirects for a list of pages that were hosted elsewhere...
EXAMPLE
If I typed in, "http://www.example.com/specificPage" - it would redirect me to a landing page at another location- "http://subdomain.example.com/specificPage". I have a WHOLE list of these pages that need to have the redirect.
I know the basic one:
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.New-Website.com/");
?>
How would that work for a big long list of say 35 of these "unique" links? Would I have to create each of them and redirect them? Or can i create just one page, maybe in connection with the HTACCESS file, and a simple code to do it for me?
Any suggestions would be greatly appreciated.
Having a SEO-agency background i can say: Do it with 35 lines in .htaccess and you will be happy with that.
We have had htaccess files with far more than 2.000 301-redirects in it from time to time and there is nothing better.
Hope, this will help...
[EDIT] Some demo code
RewriteEngine on
RewriteRule ^specificPage http://subdomain.example.com/specificPage [R=301,L, NC]
RewriteRule ^otherSpecificPage http://subdomain.example.com/otherSpecificPage [R=301,L, NC]
In some cases - depends on your specific webserver - you need to write with a ^/
RewriteEngine on
RewriteRule ^/specificPage http://subdomain.example.com/specificPage [R=301,L, NC]
RewriteRule ^/otherSpecificPage http://subdomain.example.com/otherSpecificPage [R=301,L, NC]
Put this in a .htaccess file in the root directory of the old domain:
RewriteEngine on
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
OK! I figured it out!
using both of your responses, I was able to do this in my .htaccess file
Options +FollowSymLinks
RewriteEngine on
Redirect 301 /specificPage http://subdomain.example.com/specificPage
And that worked nicely!
Thank you both, for your assistance.
Two questions,
I just finished a site and it is about to go live.
It is saved in www.example.com/store/, and I want it to strictly open at the /store/, nowhere else since there are many other areas on that drive, so first off, for achieving this action;
would I simply put inside the htaccess;
Redirect 301 / http://www.example.com/store/
??
And continuing off that, say I have links on /store/index.php that link off to www.example.com/links/ which is outside the /store/ link.. would those now not work?
Thanks for the help guys!
If you want all traffic be redirected to store/, just do:
RewriteEngine on
RewriteCond %{REQUEST_URI} !/store/$
RewriteRule (.*) /store/ [R=301,L]
Now all traffic will redirect to the store/.
Let me know if I could provide you with more details.
You could also stay with mod_alias RedirectMatch. The Redirect directive also include sub-directories, so you're actually creating a loop when you redirect / into a sub-directory. But RedirectMatch won't:
RedirectMatch 301 ^/(?!store)(.*)$ /store/
Also make sure to restrict access outside of that directory. So make only that directory available, and ban access to all others. Check this out for some help.
I have a small dilemma.
I have been working on a project for almost a year now and had an old domain name. Not knowing how Google has indexed the whole site under the old domain which has worried me. I want to put the site up on my new domain name and I think doing a 301 is the way forward. I will need to do this on every dynamic page. The good thing is the page structure is identical.
Any advice on the best way to do this would be massively appreciated.
The best way is to create a .htaccess file if you don't have one already and stick it in your html folder.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^old/page new/page [R=301,L] #can copy and paste this as many times as needed
</IFModule>
This will redirect everything from olddomain.com to newdomain.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,QSA,L]
You can put that mod_rewrite code into your olddomain's .htaccess.
If your hosting place does not support mod_rewrite (yep, there are such hosting companies) or it does not work for some strange and unknown reason, go with a simpler and widely available directive: Redirect or RedirectMatch.
Put such line into your .htaccess on old domain:
Redirect 301 / http://www.newdomain.com/
The above line will redirect ALL URLss from old domain into new domain suing 301 code (Permanent Redirect) which is the right thing from SEO perspective.
If you need to redirect such a single page or point it to a specific new location, use such rule (the same as above just specifying exact URL):
Redirect 301 /help-shipping.html http://www.newdomain.com/help/shipping.php
Apache mod_rewrite manual has a page: When NOT to use mod_rewrite. This particular scenario is listed there (Redirect is much lighter that RewriteRule in terms of resources, which can be an issue on very busy servers).
There is a way to do it via php (.htaccess is the best), but this has come in handy for me once or twice.
<?php
//this can be set however you need it to be
$dynamic_redirect = "http://foo.com".$your_variable;
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$dynamic_redirect);
exit();
?>