can i use robots.txt while handling my site with htaccess - .htaccess

I am using htaccess in my site, such that all the request to my site will be redirected to index page in my root directory. No other file in my site can be accessed because my htaccess will restrict it. My doubt is, when I use robots.txt file, will the search engines be able to reach the robots.txt file in my domain?. Or must i modify my htaccess file to allow the search engines to read the robots.txt file. If so help me in finding that specific code for htaccess.

I suppose you're using some sort of rewriting. You can exclude files from beeing processed by mod_rewrite with the following rule:
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*) $1 [L]
In this case all files that do exist in the webroot and are called directly will not be processed any further. Replace _%{REQUEST_FILENAME} with robots.txt and you should be fine.
Best wishes,
Fabian

How about trying if you can yourself access the robots.txt via a web browser? If you can, then the search engines can, an vice versa (unless you're using some IP- or browser-specific redirections or such). That is, http://yoursite.com/robots.txt

Related

Implementing 301 redirects in sub-folder of Silverstripe

I am trying to create 301 redirects from a silverstripe subdirectory to a new site/subdomain on different server, can't seem to make it work!
So need www.researchnutrition.com.au/practitioner redirecting to www.practitioner.researchnutrition.com.au
along with a number of other urls within the practitioner subfolder specified to new pages on new domain.
Have tried several combinations or rewrite/redirect rules but nothing seems to be taking effect.
Also should the .htaccess file where I make the changes need to sit within the root folder or within the /practitioner folder.
Thanks so much in advance.
You can use .htaccess file in the practitioner folder:
RewriteEngine on
RewriteRule (.*) http://www.practitioner.researchnutrition.com.au/$1 [R=301,L]
Another option is just creating a redirection page in the cms and redirecting that to an external url (which is, despite the option saying "redirect to internal page", possible).

htaccess - redirect all requests within subdirectory, except if a requested file exists

I'm developing an app in php, and I need to set up a pretty broad .htaccess redirect. I did some reading and tried to write the RewriteConds myself, but it's a bit above my paygrade - I'm hoping someone with more experience can help. Here's what I'm trying to accomplish:
The app is contained in www.example.com/app/. Don't redirect anything above this directory.
Some files exist in this directory that will need to be accessed. Currently these are /app/includes/* and /app/sb_pages/*. This will change and expand in the future, so I need an elegant solution that encompasses all existing files. It's fine if the redirect triggers within these directories when a file isn't found - all I care about is being able to access the files within without the redirect triggering.
All other requests should be redirected to /app/index.php, with the trailing url passed in the querystring. For example, a request to /app/path1/path2/ should redirect to /app/index.php?path=path1/path2/
The redirect should not be transparent. When the user requests /app/path1/path2/, I want them to believe they have remained there. They should not see the url change to /app/index.php?path=path1/path2/.
Just for added clarity, here's a few cases to elaborate:
/app/includes/sidebar.php should not redirect.
/app/includes/nothing.html does not exist - redirect is OK
/app/path1/path2/ should redirect to /app/index.php?path=path1/path2/. User should still see their current URL as /app/path1/path2/.
I hope I've explained it clearly and pre-empted most questions. If you need clarification, please don't hesitate to ask. Thanks in advance for the help!
Try adding this to your .htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^app/(.*)$ /app/index.php?path=$1 [L,QSA]
Note that if you want accesses to existing directories (as opposed to files) to also not be redirected, add a RewriteCond %{REQUEST_FILENAME} !-d above the rule.

Can one include separate files in htaccess for a list of redirects?

My main htaccess file does a bunch of things for my site to function correctly.
I have added redirects for pages that have moved. I don't have root access to the server and using .htaccess is my only option.
Is it possible to include separate files for the redirects in the .htaccess file so I can keep them separate and write programatically to the additional files that hold my redirects?
Basically I want to reference separate files from my .htaccess to manage rules dynamically and also neaten up one long .htaccess file with a few smaller files.
I also want to add redirect rules on the fly as things change on the site within my application.
You can use a RewriteMap http://httpd.apache.org/docs/2.3/rewrite/rewritemap.html
Let's say your map file looks like this and is called moved.map:-
/about profile
/page/that/has/moved new/location
You .htaccess would need something like this:-
RewriteMap moved txt:moved.map
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond ${moved:%1|NOT_PRESENT} !NOT_PRESENT [NC]
RewriteRule .? ${moved:%1} [NC,R=301]
This will redirect with a 301 status code http://your.domain.com/about to http://your.domain.com/profile and redirect http://your.domain.com/page/that/has/moved to http://your.domain.com/new/location
You can then programmatically create moved.map.
I hope that helps.
If you are using .htaccess files then don't bother with RewriteMap -- it only applies if you have root access to the server or vhost config, which is never the case when you purchase a shared service offering.
If you are constrained to use .htaccess files then you have two options:
The first is to do what some packages do and that is to get your application to rewrite the .htaccess file based on a rewrite map that you maintain within in it. The best way to do this is to have "bookends" in your .htaccess file e.g.
##++AUTOMATIC rewrite rules
<rules inserted by your app>
##--AUTOMATIC rewrite rules
And when an update occurs have your app read in the .htaccess, swap out the section between ##(++|--)AUTOMATIC rewrite rules, write it back to a temp file, then move the temp file to .htaccess (this makes the rewrtie-back atomic on *nix OSs).
The second which might work if you know some regexp regular pattern which covers the rewrites (this is often the case) then use a rule to map them to a redirector script which looks up the new target and itself issues a:
$server = $_SERVER['HTTP_HOST'];
header( "Location: http://$server/$newTarget?$parameters", TRUE, 301 );
Note the 301 redirect -- this means that client browsers should cache this and remember this in future.

htaccess selective redirect

I am trying to redirect all sub-directory pages to main directory, except of a few pages (e.g. (somepage1.html).
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(de|ru)/somepage1.html
RewriteRule ^([a-z]{2}|zh-CN|zh-TW)/(.*)$ /$2 [R=301,L]
Everything working except de/somepage1.html is redirected to home page (/), which is not acceptable. I wont it not redirected at all.
How can I achieve it
Thanks1
Well above rules are clearly excluding de/somepage1.html URL so it is most likely some other rule that is redirecting de/somepage1.html to /. Are you using wordpress or some other CMS tool by any chance? That might have its own rules in .htaccess file, check that please.
Also it would help to check web server's access log when this redirection happens.
the code I provided is working perfectly, so if somebody looking for this kind of a solution can use it without a fought.
The reason for not working is that my website were keeping cash and therefore was not renew frequently.

using htaccess to set default folder instead of file?

Is it possible to set a default folder to access instead of a file like "index.html".
What I'd like to to is make it so that when a person visits my site they get redirected to a folder within the root of the domain. I am using a blogging engine and I need it to show up as the homepage but I don't want to install it in the root because I have other folders and files that need to be in the root directory. And I don't want to put them inside the blogging software's folder. I also don't want to use a 301 or 3XX redirect for SEO purposes.
If there's a way to do what I'm asking let me know. If not, let me know the best option otherwise.
Try this mod_rewrite rule:
RewriteEngine on
RewriteRule ^$ foo/bar [L]
This will rewrite requests to the directory where this rule is applied to to foo/bar. So if you put this rule in the .htaccess file in your document root and request http://example.com/, it will get rewritten to http://example.com/foo/bar.

Resources