Proper htaccess redirect - php

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.

Related

PHP Permanent Redirection

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

301 redirect in .htaccess for 30,000 errors

I've been tasked to clean up 30,000 or so url errors left behind from an old website as the result of a redesign and development.
I normally use .htaccess to do this, but I doubt it would be wise to have 30,000 301 redirects inside the .htaccess file!
What methods have some of you used to solve this problem?
Thanks in advance.
Here as you can do with apache httpd
RewriteMap escape int:escape
RewriteMap lowercase int:tolower
RewriteMap my_redir_map txt:map_rewrite.txt
RewriteCond ${my_redir_map:${lowercase:${escape:%{HTTP_HOST}%{REQUEST_URI}}}} ^(.+)$
RewriteRule .* http://%1 [R=301,L]
I use this rewrite rules usually directly inside apache httpd configuration.
Inside the map_rewrite.txt file you have a tab delimited file with the list of redirect in the following format:
www.example.it/tag/nozze www.example.it/categoria/matrimonio
www.example.it/tag/pippo www.example.it/pluto
www.example.it/tag/ancora www.google.com
Would be much easier if you can generalize the approach because the redirect have a common pattern. But if not, in this case you only need to add the redirected url into the list.
Take care to study the RewriteMap configuration, because you can also write the list into a different format, for example like a database table.
Please pay attention to this: I have added escape and lowercase only because there are accents into the urls I need to write. If your urls doesn't have accents, you can remove both.
If you want implement these redirects in php, here the code you need:
<?php
$dest_url = "http://example.com/path...";
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$dest_url);
Create a PHP page to operate as a 404 handler. It should inspect the incoming URL, check if it should map from an old page to a new page, then issue a 301. If there is no mapping then present a 404.
Simply set this page as the 404 handler in your .htaccess and there you go. IIRC this is how Wordpress used to handle 'clean' URLs on IIS before IIS7 brought in URL rewriting without needing a 3rd-party dll.
I have made a redirect class that is on the 404 page that will check the database if there is a valid page to 301 redirect to and redirect it instead of giving the 404 page. If it can't figure that out, it marks it in the database as a 404 page, so it can be fixed later.
Thanks for help guys. I've carried out the suggested course of action from freedev but have created a separate config file within Apache.
Within the httpd.conf file I have added:
# Map settings
Include "conf/extra/map.conf"
The map.conf file:
RewriteEngine On
RewriteEngine on
RewriteMap url_rewrite_map txt:conf/map.map
RewriteCond ${url_rewrite_map:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*) http://website.com/${url_rewrite_map:$1} [R=301]
The map.map file is formatted as:
/oldname/ /newname
I've added quite a few of the urls for the redirection and so far so good, it isn't having a massive impact on the server like it did when added to .htaccess

.HTACCESS - redirect www.example.com/side.php?id=* to www.example.com

I have recently assisted in moving a website from a pure development domain to a live site where there used to be a site handled by another CMS system than what we are currently using. Current system is Joomla, but I don't think it matters much for my question.
So with the current site the URLs are rewritten from the standard Joomla format to be stripped of index.php and .html suffix is added in the address, meaning that URLs look like this:
http://example.com/folder/page.html
In the old site handled by another CMS systems the URLs had the following structure:
http://example.com/side.php?id=1
We are a social organisation with many sites linking to us - also quite a few that we are not even aware of - so the problem I need to handle is this: I need to redirect all these dead links on other sites so that they simply get pointed to the root of our site.
Can anyone please explain to me how to make .htaccess redirect as follows:
/side.php?id=* to root of example.com
In this case I mean the * to mean any number as there are naturally alot of pages with different IDs.
It is not of any significance to me if they point at a www. prefix or not.
Thanks in advance for your help, I hope I have not asked a question that's been answered before but my experience with .htaccess is very limited and having searched and tried different solutions didn't do it for me.
In .htaccess in the root folder, add the following:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=\d+
RewriteRule ^side\.php$ http://%{SERVER_NAME}/ [R=301,L,QSD] #Remove the ",QSD" for Apache <2.4.0, or to keep the query string.
The R=301 will tell browsers/search engines that the page has permanently been moved.
A rule like this should work:
RewriteCond %{HTTP_HOST} .*
RewriteCond %{QUERY_STRING} id=\d+
RewriteRule ^side.php http://example.com [L,R=301]
This will redirect externally, with a HTTP 301 response (Moved Permanently)
What you need here is some 301 Moved Permanently responses to let browsers and search engines know that you're moving pages to a new location. Instead of using a mod_rewrite to redirect all requests to this side.php page, I would analyze each page of your old website and determine where all the content has been moved. Armed with this, use the htaccess Rewrite directive to inform browsers of individual pages being moved
Redirect 301 /side.php?id=123 /about_us.html
Redirect 301 /side.php?id=456 /contact_us.html
I recommend this method because it redirects those who navigate to the outdated page to the new page that has similar content to the old one they were requesting instead of just redirecting them all to your home page.

how to redirect to another page if file exist in root?

I'm trying to make a script to put on all my pages on my site saying if so and so file exist in the root directory ("/") it will auto redirect to it and if the file isn't there it does nothing.
I'm using this so i can set up a maintenance mode for the site so i can take it down while im working on it. I already have made the maintenance page, I just don't know how to set up the script. The file name is maintenance.html and I only want it to be in the root file. I don't want to have to upload it to every directory to take the site down.
The file url would be http://domain.tld/maintenance.html and the script would go if the file is there and redirect to that file else if it's not there don't redirect.
I know the redirect code is (in HTML)
<meta HTTP-EQUIV="REFRESH" content="0; url=http://domain.tld">
You should try this in your .htaccess file:
RedirectPermanent / /maintenance.html
RedirectPermanent /page2.html /maintenance.html
RedirectPermanent /anotherpage.html /maintenance.html
And so on. So just do this for each page of your site, on a new line for each.
This will redirect each of your pages right away to the maintenance page.
.htaccess is the best way to do it in my opinion. (better than JavaScript)
Hope this helps.
EDIT:
To use it, first you put:
RedirectPermanent
And then a space and then the page you want to redirect to the maintenance page:
/page.html
And then another space and then the page you want to redirect to:
/maintenance.html
So, all together, here's an example:
RedirectPermanent /page.html /maintenance.html
Note the space in between RedirectPermanent, the page redirecting from and the page redirecting to.
The way it works, well I don't know. This isn't a script, it's a .htaccess file code.
You can use:
if(file_exists('/file.php'))
{
//do something if file exists
}
The better way would be the put a .htaccess named file in your root folder with the following content:
ErrorDocument 404 /maintenance.html
This redirects automatically to this page, if the called page is not existing.
A set of redirection rules for your webserver is what you need, methinks. If you're running Apache, mod_rewrite is the magic word, if you're running something else, well, then, I wouldn't know the magic word, but something similar exists for most servers, if not all.
But, using Apache's brilliant mod_rewrite, to redirect ALL traffic to a set page or address, e.g. during maintenance, is as simple as:
<IfModule mod_rewrite.c>
# Use mod_rewrite
RewriteEngine on
# If you want, you can exclude yourself by adding a condition for the redirection,
# i.e. if the RewriteCond matches, proceed with the RewriteRule
# This statement checks that the IP of the client isn't 123.456.789.012
RewriteCond %{REMOTE_ADDR} !123.456.789.012
# Redirect all traffic to /maintenance.html with a "307 Temporary Redirect",
# except traffic to the maintenance page.
RewriteCond %{REQUEST_FILENAME} !maintenance.html
RewriteRule .* /maintenance.html [R=307,L]
</IfModule>
Where should these instructions be, you ask? Well, since it's a temporary thing, the most logical would be in a .htaccess file in your webroot. But it's also possible to include the same in your servers/virtualhosts global configuration, which for a permanent ruleset would make sense from an optimization aspect.
To disable the redirection, it's enough to comment out either the RewriteEngine on statement, or the RedirectRule statement. You could also rename your .htaccess to something else or delete it.
It's not very efficient to write your own server-side script to check for a file when your webserver can do it for you. Use Apache's mod_rewrite capability in an .htaccess file; you'll enable (i.e. uncomment) your rewrite rules when you want to put your page in maintenance mode. Doing it this way would also allow you to access the website while you work on it if you put in a rule to allow access from your own IP.
If this is free hosting -- which it seems like it is -- then you may not be able to do this, but I don't see why it would be a major issue to do it. Most webserver software has some sort of rewrite function, and this is a fairly trivial rewrite.
Alternatively you could use a quick-and-dirty bit of Javascript similar to this (might not be exactly this):
<script type="text/javascript">location = www.yoursite.com/maintenance.html;</script>
It'd be better to use rewrites, though.

htaccess 301 redirect url

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();
?>

Categories