I have a system that handles multiple domains under one file structure and each domain would need different sitemap and robots.txt.
For sitemap I have set up a redirect and I know it works well. I would like to confirm that same is possible with robots.txt?
I have added a rewrite rule in .htaccess that redirects person to a php page. On this php page I find what domain user has and print out correct information with text header.
Is this allowed?
Extra info:
I have a codeigniter application that is used by domainA and domainB. while domainA should see robots for domainA, domainB should see robots for domainB. And if i am to create robots.txt in the root of the site both domainA and domainB would have access to it, due to that I have created a separate php page to give out correct robots for domainA and B.
In .htaccess i have a rewrite rule similar to:
RewriteRule ^robots.txt$ func/getRobots/$1 [L]
After looking around I was able to identify that other people do it:
http://www.aberdeencloud.com/docs/guides/how-use-different-robotstxt-development-and-live-environments
.htaccess and robots.txt rewrite url how to
https://webmasters.stackexchange.com/questions/61654/redirect-google-crawler-to-different-robots-txt-via-htaccess
I just want to be sure that it wouldn't damage SEO side of the system.
You damage your SEO when search engines are not capable of accessing your robots.txt at all.
Not using standard practices regarding the location of your robots.txt is equivalent to taking a risk. Some search engines may handle your case well, others may frown and find it suspicious.
In one of the links you mention, a solution is suggested to avoid rewriting the URL. I would stick to it.
Related
I work as a web developer, creating custom websites for customers. My .htaccess files are generally very simple. I generally only do 2 things:
Create a custom ErrorDocument for 404 errors
Redirect the /upload directory to /admin/upload to simplify some shared code between the public website and the admin system
I have everything working okay, other than one slight annoyance I'm wondering if anything can be done about. I'm using php, and I try to design the websites in a modular fashion, so that it doesn't matter whether the code is hosted at "example.com/" (the real domain), "localhost/exampleproject2" (on my development machine), or "mycompanywebsite.com/example.com" (Where we host a "beta" version for the customer to view when we get to that point in development).
From the PHP side of things, I have no issues here. But so far as Apache is concerned, I have to create 3 slightly different .htaccess files for each of those domains:
ErrorDocument 404 /404.php
Redirect 301 /upload /admin/upload
ErrorDocument 404 /exampleproject2/404.php
Redirect 301 /exampleproject2/upload /exampleproject2/admin/upload
ErrorDocument 404 /example.com/404.php
Redirect 301 /example.com/upload /example.com/admin/upload
Obviously this is not a huge deal, but it is a bit annoying. Mostly when I or someone else transfers files over FTP and accidentally overwrites the .htaccess from one environment with it's counterpart from another environment, and the site breaks. Also it means I can't include .htaccess in my git repository without possibly breaking things.
From my testing, relative urls don't seem to work at all. I think maybe Apache just doesn't support relative urls in .thaccess files for Redirects and ErrorDocuments. At least that's the way it seems. I get a server error when I try.
I'd like a way to have a single .htaccess file I can share between all 3 environments and include in the git repository. If anyone knows a good way to accomplish this that would be awesome. Solutions don't have to use .htaccess, I'm open to any solution that makes my code less dependent on absolute paths or urls.
I have found a lot of information how to redirect example.com to www.example.com. Two methods DNS and .htaccess. The problem is that most answers do not provide hosting service type and seems different people recommend different option. I know .htaccess is no brainer for Shared Hosting.
Does anybody know which method is better for VPS?
In order of least to most advisable places to issue an HTTP 30X:
DNS
Well, it's impossible to perform an HTTP redirect with DNS, so this is right out.
Application [eg: in PHP]
You can do it here, but really you want to do as little host-based muckery in the application, mostly of the sake of separation of concerns.
.htaccess
You can totally do it here, but you probably shouldn't use .htaccess at all if you can avoid it.
Server/Vhost Config
Yes, this is where you should do it. Rewrites and redirects are parsed once at server start, and handled by highly-efficient code.
Very Late Edit:
I'd like to add that performing redirects in the application code is not necessarily a bad thing, but for simple www. and other host/infrastructure-level redirects the simplicity can't be beat.
eg: foo.com to www.foo.com and :80 to :443
However, if you're also doing application redirects you need to have a bulletproof division between what redirects are handled by the the server and which are handled by the application. Otherwise you can descend into a circle of hell where the application and server disagree on how to redirect.
If that division is anything less than absolute then you're probably better off simply letting the application manage redirects entirely.
These types of suggest questions are frowned upon, but, the domain registry should handle both WWW and non-WWW domain names. Then at your server, you can choose to respond to both, or 301 redirect one to another via htaccess
i read the other threads about this topic but i couldn't find anything that i felt could apply in my case. It seams pretty basic though. I have a website built in php with apache server. In this moment all the traffic is done via http. The people who paid for the site, now want to move it to https. They bought a certificate and the web server hosts will install it. What changes do i need to make to make it work via https, besides changing the redirects within the code?
I also found this link which seems pretty helpful, but i think maybe it's too complex?
Thank you,
Alex
You should change your resource links (like external JavaScript references such as jQuery) in the site where there are hard-coded paths in http://domain.name/some-link-here to just //domain.name/some-link-here. This will prevent the browser from complaining about mixed-mode content.
For links that are on the same domain, you could use absolute/relative URLs.
After that you can place and .htaccess such that any URLs accessed on the domain would automatically redirect to the HTTPS version. Place the following lines as the first rule in the file
.htaccess code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The .htaccess will also take care of any hard-coded links (towards the same domain/site) that you might have in your site and that you have missed.
I understand that there may be other questions regarding vanity urls but everyone i see has a different code that i guess does the same job. Therefor i do not understand what rules are best for my personal question, that being said here is my question.
I simply want to create this,
127.0.0.1/website/profile.php?id=1
To this,
127.0.0.1/website/profile/Admin
My sub questions are also,
I understand the .htaccess file has to be in the root directory, but is that the root of my website or my xampp htdocs?(e.g c:/xampp/htdocs/ or c:/xampp/htdocs/website)
Using php should i make the conversion between id to username for the URLS on a seperate file then redirect to the requested user's page?
Thank you for reading, i just can't seem to get my head around .htaccess!
Root directory of your website.
No need for redirects. The way it works is that you can map every section of your URL to a URL parameter. For example, http://localhost/profile/Admin is really interpreted as http://localhost/website/profile.php?username=Admin. Only users will see the vanity URL; PHP will still see the URL parameters. In your case, the .htaccess rule will look something like ^profile/([0-9]+)$ profile.php?username=$1 (I obviously don't know for sure since I don't know the architecture of you site).
On a side note you might find Virtual Hosts interesting. It's a way of being able to create your own local domain for your site, for example http://my-local-website instead of using http://localhost/website or waiting to test in production.
More info here: http://sawmac.com/xampp/virtualhosts/
I have been looking for a while now to find a solution to accomplish the following system.
I would like to build a PHP system on, let's say, domainA. On this domainA I will allow an administrator to 'create' a new website. This website contains only of pieces of text, which are all stored in a database. This, I know how to do.
However, now I would like to make it possible that visitors that surf to domainB will be invisibly redirected to for example domainA.com/gateway.php?refdomain=domainB&page=xxx or something similar.
I have a vague idea this should be done by .htaccess, but I don't really know how I can do this in the most easy way. If for example there are some POST or GET requests on domainB, this should keep working. Also images linked to http://www.domainB.com/test.gif should be invisibly loaded form www.domainA.com.
I also know there are some CMS systems (eg drupal) which allow this feature, so it is possible, I just don't know how.
Thank you for any advice that might point me in the right direction,
kind regards,
Digits
Are you hosting both of these on the same machine? If so, something like VirtualHosts in Apache could solve this for you.
mod_alias and mod_rewrite might also be of some use to you.
Basically, you'll want to point all your domains to the same directory (maybe using a wildcard in your vhosts) and then setup urlrewrite; look at this question for an example, and it can be in a .htaccess file or Apache configuration.
All requests that come in will go to the same gateway.php and you can extract the current domain and requests using $_SERVER['REDIRECT_QUERY_STRING'], $_SERVER['REQUEST_URI'] and $_SERVER['SERVER_NAME'] for example. See $_SERVER. You'll then be able in your gateway.php to send the correct files.
If you use a CMS like Drupal, you should be able to assign these using the Portal Alias. By using the alias you will be able to assign different domains to point to different "sites" that are created.
OK, here's a really simple example:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domainA\.com
RewriteRule (.*) http://www.domainA.com/gateway.php?realpath=$1 [L,QSA]
You could then parse "realpath" in your gateway script using parse_url and take the appropriate actions.
You could get more complex with your rewrite rules to have separate ones for images, etc. if you wanted to
You could use the redirect header..