Sorry to be asking yet another htaccess question but I have searched at length and can't crack it.
I have changed my website to a single page, www.mysite.com/index.html. Previously there were subdirectories and sub-subdirectories, all with a combination of html and other files in them.
I now want all file requests to be directed to the homepage, and for the homepage to display in the browser bar. Eventually I want to remove the old directories and images, but I'm assuming for now that Google needs me to keep them in order to transfer any ranking (is this right?).
There is just one exception to the rule that all traffic should go to www.mysite.com. I have bought another domain www.mysite2.com for SEO reasons. Currently this site has no hosting and is set to redirect to www.mysite.com. If users find www.mysite2.com, I would like them to see content from www.mysite.com but display the domain name www.mysite2.com.
This is the htaccess code I am currently using:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^?]*)$ /index.html?path=$1 [NC]
Options -Indexes
IndexIgnore *
RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule ^(.*) http://mysite2.com/$1 [P]
I have several questions/problems:
1) the www.mysite2.com redirect threw a 404 file not found error when i tried to access www.mysite.com. (I am having to test in live production... not great).
2) typing in www.mysite.com/products/ (former directory, still existing) takes me to www.mysite.com/// - it works but is unsightly.
3) typing in www.mysite.com/products/subdir/file.html still takes me to that original file
4) typing in www.mysite.com/lsdhfskdhf (i.e. a non-existent location) displays the homepage but continues to show the string.
5) typing in www.mysite.com/products/ksjdhfkdsjfh (i.e. non-existent location) displays the homepage but without any css formatting (!).
6) Should i be using Redirect 301 instead of RewriteRule? I heard that Google only likes 301 redirect.
I am a bit stuck and would welcome any help! Even if there is a comprehensive guide somewhere to whatever language apache is using in its directories that would be good. Lots of sites claim to this but i haven't found a good one.
Apologies for length.
Thanks
Emma
Related
Ok, so I know this is a question that has been asked many times, however, I have not been able to find an answer to my particular case, so please do not shoot me down.
I have a website: http://gmcomputers.co.za.
I am redirecting this URL, using .htaccess file, to a subfolder to load the content:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) /gmcomputers/ [L,DPI,R=301]
Which works perefectly, except when I go to http://gmcomputers.co.za I get http://gmcomputers.co.za/gmcomputers/.
So my question is, how do I modify the above code to remove the /gmcomputers/ from being appended?
Please note I copied the code above from a website as I am not at all experienced in redirect, etc and am still learning. Also, the reason I am using .htaccess to redirect is due to there being other websites in the root directory and I therefore cannot edit any config files for Apache.
Thanking you.
You contradict yourself in your question. On the one hand you write that you want to redirect and that this "works perfectly", but then you write that you do not want that result.
My guess is that you actually do not want to redirect at all, but that instead you want to internally rewrite your requests to point to that server side folder. While the URL visible in the browser's URL bar does not show that folder. Is that what you are trying to ask?
If so take a look at this example:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/gmcomputers
RewriteRule ^ /gmcomputers%{REQUEST_URI} [END]
You might want to add an actual redirection to direct clients actually using the folder name in their requests:
RewriteEngine on
RewriteRule ^/?gmcomputers/(.*)$ /$1 [R=301,END]
RewriteCond %{REQUEST_URI} !^/gmcomputers
RewriteRule ^ /gmcomputers%{REQUEST_URI} [END]
Best is to implement such rules in the central http server's host configuration. If you do not have access to that you can instead use a distributed configuration file (typically called ".htaccess") located in the DOCUMENT_ROOT folder configured for the http host, if you enabled the consideration of such files in your host configuration . Though that comes with a number of disadvantages. Above implementation works likewise for both approaches.
As of now, my htaccess looks like this
RewriteEngine On
RewriteCond %{QUERY_STRING} ArticleID=([^&]+)
RewriteRule ^article.cfm$ /articles.php?id=%1 [R=301,L]
RewriteRule ^([a-zA-Z0-9-]+)$ page.php?slug=$1
RewriteRule ^([a-zA-Z0-9-]+)/$ page.php?slug=$1
ErrorDocument 404 "<H1>Page not found</H1>"
The first 3 lines allow me to redirect users who are access articles through .cfm to .php instead
For the 4th and 5th lines, my website allows creation of pages through a WYSIWYG editor and allow users to access it. The actual url will look like www.website.com/page.php?slug=homepage but after the rewrite rule, it will look like www.website.com/homepage
However, I was having problems where my admin pages such as www.website.com/admin/ was regarded as one of the page so I added the below codes and it works
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
But I'm still having a couple of problems.
If you access an invalid page such as www.website.com/hello/ when there is no such page created by the users at all, it will still show page.php but with empty contents, how do i redirect it to a 404 page?
How do I secure directories which does not have an index.php page? such as my images folder, includes folder, javascript folders etc?
Does my entire htaccess looks right at this current point?
Thank you guys so much for helping! I'm not good with mod_rewrite so I will appreciate your help!
If you access an invalid page such as www.website.com/hello/ when
there is no such page created by the users at all, it will still show
page.php but with empty contents, how do i redirect it to a 404 page?
You should do this with PHP. If there's no content included, use header() to redirect to a 404.
How do I secure directories which does not have an index.php page?
such as my images folder, includes folder, javascript folders etc?
Add this to your .htaccess: Options -Indexes
Does my entire htaccess looks right at this current point?
Looks fine.
I have an old website that uses links such as:
www.oldsite.com/pages.asp?pageid=123456
And have a new website that uses links such as: newsite.com/about
The old site appears in search results like that and I need to rewrite the owld URLs to the new site. I have searched for hours and hours and tried multiple things (none have exactly matched my situation) concerning RewriteRules and such and none have worked. I'm using
redirect /pages.asp /
now as a temporary solution, but I need the various links in search results to go to the new location (as in ...id=12345 goes to /about).
Any help would be greatly appreciated. Thank You!
Clarification:
The old website is completely out of my control, it is 301 redirecting to the new site. So the actual URLs I want to redirect are in the form newsite.com/pages.asp?pageid=123456.
I want to redirect the old pages to matching new pages. Some Examples:
www.newsite.com/pages.asp?pageid=123456 >> newsite.com/about
www.newsite.com/pages.asp?pageid=85544 >> newsite.com/contact
If you have only 10 pages, you can do it with simple rules instead of a map.
You can put this code in your htaccess (which has to be in root folder)
RewriteEngine On
# example rule for "about"
RewriteCond %{QUERY_STRING} ^pageid=123456$ [NC]
RewriteRule ^pages\.asp$ /about? [R=301,L]
# example rule for "contact"
RewriteCond %{QUERY_STRING} ^pageid=85544$ [NC]
RewriteRule ^pages\.asp$ /contact? [R=301,L]
# and so on for your other pages ID...
I'm trying to rewrite all 404's to index.php where I use PHP's parse_url() to determine which file to include (e.g. about-us.php, contact-us.php) and I'm getting some really weird results.
I'm working on a 'dev' URL automatically created by cPanel:
http://xxx.xxx.xxx.xxx/~mySite/
Current Method
My .htaccess file contains the following:
RewriteEngine on
RewriteBase /~mySite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_.]+)$ index.php
And the results are a mixed bag:
xxx.xxx.xxx.xxx/~mySite/contact-us renders just fine.
xxx.xxx.xxx.xxx/~mySite/contact-us/phone throws a 404 that isn't caught by mod_rewrite.
xxx.xxx.xxx.xxx/~mySite/about-us rewrites to the server root (xxx.xxx.xxx.xxx/index.php).
Previously Tried
ErrorDocument 404 /~mySite/index.php
And I get the similar results, except for the following:
xxx.xxx.xxx.xxx/~mySite/contact-us/phone rewrites to index.php but all my CSS and JS includes are off because they're trying to load relative to /~mySite/contact-us instead of /~mySite.
Any help? I'm going out of my mind. Especially the fact that contact-us works fine, but about-us doesn't?
First don't use cPanel preview. That is not a good way to view your dev site. Who knows how it will affect the rules. Also control panels do weird things anyway.
Preview your site using your real site domain name. You can do that my modifying your HOST file on your computer so that only you can view it by the domain name. This little guide will show you how to edit it. It takes like 2 minutes.
Then that should help to check things better.
Most likely why the error document doesn't work because ~mySite is most likely not your document root. That is typically how cpanel does it's preview links. So your real error document should probably be as Marc B stated.
ErrorDocument 404 /index.php
If you want a mod_rewrite solution, this should also work. But I wouldn't use both.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [R=301,L]
On a side note, I think it's usually good to use a 404 page so that users know that the requested page is not a real page instead of some people thinking something is just wrong with your site because it redirects to the home page. Like facebook, it gives you a big thumbs up with a bandage on it saying it the page is not available. I've seen many custom 404 pages that were pretty clever so maybe it might be time to just get creative.
I am trying to get a redirect set up for my site. I have three subdomains which I use
1) (www.)mysite.com
2) sub.mysite.com
3) anothersub.mysite.com
I initially didn't use subdomains and had all my pages in a www.mysite.com/*.php format.
I am trying to add a redirect to make all the pages under the /myfolder/ folder redirect to sub.mysite.Com/ (NB sub is the same name on my site as the myfolder folder)
I have tried setting up a redirect through cPanel but all the combinations have resulted in 404 errors when I try them. I have also tried various things in .htaccess which resulted in many 404 errors.
I added a simple redirect in cPanel and get the following
e.g.
www.mysite.com/myfolder/myfolder_mypagename.php
should redirect to
sub.mysite.Com/myfolder_mypagename.php
but instead it redirects to
sub.mysite.com/_mypagename.php (missing out the "myfolder" part of the pagename)
Can you assist me in this please? It's driving me bonkers and my hosting company said it wasn't possible (which I find hard to believe!)
This answer is assuming your are facing the issue that, when creating a subdomain through cpanel and pointing it to the desired directory, some part of the urls get striped.
Create you subdomain through cpanel, and make it point to the root directory (just like www.mysite.com), then use this in your htaccess :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$
RewriteRule ^myfolder/(.*)$ http://sub.mysite.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^sub\.mysite\.com$
RewriteRule ^(.*)$ myfolder/$1 [L,QSA]