The Hidden Files That Control Your Website: Understanding .htaccess and robots.txt
Every website has invisible files working behind the scenes to control how it behaves. Two of the most important are .htaccess and robots.txt. While you’ll likely never edit these yourself, understanding what they do helps you communicate with your web developer and protect your website from common problems.
Think of these files as your website’s security guards and traffic directors—they decide who gets in, where they can go, and what they can see.
What Is .htaccess? Your Website’s Rule Book
The .htaccess file (yes, it starts with a period) is like a set of instructions that tells your web server how to handle various situations. It’s powerful but dangerous—one wrong character can break your entire website.
What .htaccess controls:
- Where visitors go when they type your URL
- What happens when someone finds a broken link
- Who can access certain parts of your site
- How fast your website loads
- Security rules to block hackers
- URL structure and redirects
Common uses you should know about:
1. Redirecting Old Pages (Preserving SEO Value)
When you redesign your website or change page names, the .htaccess file ensures visitors (and Google) find the new pages.
Example scenario: Your old “services.html” page is now “our-services”
Without a redirect, anyone visiting the old URL gets an error. With .htaccess, they’re automatically sent to the new page, preserving your search rankings and user experience.
2. Forcing Secure HTTPS Connection
That padlock in your browser’s address bar? The .htaccess file can force all visitors to use the secure version of your site.
Why it matters:
- Google favors secure sites
- Browsers warn users about non-secure sites
- Protects customer data
- Builds trust
3. Creating Clean URLs
Instead of: yoursite.com/page.php?id=123&category=services You get: yoursite.com/services
Clean URLs are:
- Easier to remember
- Better for SEO
- More professional looking
- Shareable on social media
4. Custom Error Pages
When someone hits a broken link, instead of seeing a generic error, they see your branded 404 page with helpful navigation.
Good 404 pages include:
- Apology for the inconvenience
- Search box
- Popular pages links
- Contact information
- Maintains your design
5. Blocking Bad Bots and Hackers
The .htaccess file can block:
- Known spam bots
- Specific IP addresses attacking your site
- Countries (if you only serve locally)
- Automated hacking attempts
Signs you’re being attacked:
- Slow website performance
- Strange files appearing
- Spam comments/form submissions
- High bandwidth usage
6. Speed Optimization
Browser caching rules in .htaccess make your site faster:
- Images cached for weeks
- CSS/JavaScript cached for days
- HTML refreshed frequently
- Reduces server load
Impact: Pages load 50% faster for returning visitors
What Is robots.txt? Your Search Engine Guide
While .htaccess controls server behavior, robots.txt specifically talks to search engines, telling them what to index and what to ignore.
Location: Always at yoursite.com/robots.txt (publicly visible)
Common robots.txt Uses
1. Preventing Duplicate Content Tells Google not to index:
- Print versions of pages
- Admin areas
- Shopping cart pages
- Internal search results
2. Protecting Sensitive Areas Blocks search engines from:
- Login pages
- Thank you pages
- Development/staging areas
- Private directories
3. Managing Crawl Budget For large sites, directs Google to important pages:
- Prioritizes product pages over filters
- Focuses on fresh content
- Prevents wasting resources
4. Sitemap Location Points search engines to your XML sitemap: Sitemap: https://yoursite.com/sitemap.xml
Real Examples You Should Understand
Basic robots.txt for most businesses:
User-agent: * Disallow: /admin/ Disallow: /private/ Disallow: /thank-you/ Allow: / Sitemap: https://yoursite.com/sitemap.xml
This tells all search engines: “Index everything except admin, private, and thank-you pages. Here’s my sitemap.”
When pages move permanently (.htaccess redirect):
Redirect 301 /old-page https://yoursite.com/new-page
This permanently redirects the old page to the new one, preserving SEO value.
Warning Signs Something’s Wrong
With .htaccess:
- Site suddenly shows 500 errors
- Pages redirect to wrong locations
- Site becomes extremely slow
- Security warnings appear
With robots.txt:
- Pages disappear from Google
- Search traffic drops suddenly
- Google Search Console shows errors
- Important pages aren’t indexed
What Business Owners Should Do
Regular maintenance checks:
- Monthly: Check Google Search Console for errors
- Quarterly: Review 404 errors in analytics
- Annually: Audit redirects and security rules
- After changes: Verify everything works
Before a redesign:
- Document all current URLs
- Plan redirect strategy
- Preserve SEO value
- Test thoroughly
Security best practices:
- Keep .htaccess file backed up
- Monitor for unauthorized changes
- Review access logs monthly
- Block suspicious IP addresses
When to Call Your Developer
Immediate attention needed:
- Site showing 500 errors
- Hacked site symptoms
- Sudden traffic drop
- Pages not indexing
Planned maintenance:
- Before redesigning site
- Adding new sections
- Changing domain names
- Implementing security features
Questions to Ask Your Developer
About .htaccess:
- “Are we redirecting old pages properly?”
- “Is our site forcing HTTPS?”
- “Do we have custom error pages?”
- “Are we blocking bad bots?”
- “Is browser caching configured?”
About robots.txt:
- “What are we blocking from search engines?”
- “Is our sitemap referenced?”
- “Are there any crawl issues?”
- “Should we adjust for better indexing?”
The Cost of Getting It Wrong
Poor .htaccess management:
- Lost search rankings from broken redirects
- Security breaches from lack of protection
- Slow site from no caching
- Poor user experience from generic errors
Poor robots.txt management:
- Important pages not indexed
- Duplicate content penalties
- Wasted crawl budget
- Private information exposed
The Bottom Line
You don’t need to understand the technical details of .htaccess and robots.txt files, but knowing they exist and what they do helps you:
- Communicate with your developer
- Understand website problems
- Make informed decisions
- Protect your investment
These files are like your website’s DNA—small changes can have big impacts. Always have your developer handle edits, keep backups, and monitor for issues.
Your website is more than what visitors see. These hidden files ensure it runs smoothly, ranks well, and stays secure. Respect their power, and they’ll serve you well.
If you have web development questions, or are in need of having a website developed, please feel free to contact me at info@ecurtisdesigns.com.







HTACCESS for Mobile Redirect and Back question. Not a programmer, so have no clue what code means.
If instead of redirecting to a subdomain like in your example from https://yourdomain.com to https://m.yourdomain.com, how do you make it work with 2 separate domain names? Absolute urls?
What I want to do is redirect from desktop site with domain https://virtualmarcom.com to mobile site with https://virtualmarcom.mobi and allow link back to desktop site https://virtualmarcom.com without redirect. Only changes needed in lines below? What would they be? Also, does it require RewriteEngine Off in .htaccess file for https://virtualmarcom.mobi? Anything else?
RewriteCond %{REQUEST_URI} !^/m/.*$
RewriteCond %{HTTP_REFERER} !^https://(.*).yourmaindomain.com/.*$ [NC]
RewriteRule ^(.*)$ /m/ [L,R=302]
Hello Eric. The rewrite rule is what you need to focus on. Note at the bottom which reads: RewriteRule ^(.*)$ /m/ [L,R=302]
Being new to .htaccess, I would suggest reading up a bit. There are a lot of websites that explain htaccess mod rewrites. For example:
RewriteEngine On
Redirect 301 https://www.nonmobilesite.com https://www.newmobilesite.com
This provides a permanent redirect from one url to another.
Ok, so for the above “M” subdomain rewrite, to link to another URL instead independent of your existing website, you would have:
RewriteRule ^(.*)$ https://www.mobilewebsite.com [L,R=302]
You could also link to a folder in the new website such as:
RewriteRule ^(.*)$ https://www.otherwebsitewebsite.com/mobile/mobile.html [L,R=302]
Edmund–
Thanks for your response.
Permanent 301 is not what I want. That’s easy enough. It’s the allowing the user back to the desktop version of the site if they so choose. Common client request lately. My client’s desktop/iPad site is nicholsonvineyards.com and the mobile site is nicholsonvineyards.mobi. Two completely different websites and not a subdomain.
So I made only this change to the last line:
RewriteRule ^(.*)$ https://www.nicholsonvineyards.mobi [L,R=302]
And it doesn’t redirect to the mobile site unless you reload the browser on my iPhone. Then it redirects to https://www.nicholsonvineyards.mobi after reload.
2nd line:
RewriteCond %{REQUEST_URI} !^/m/.*$
and
2nd to last line:
RewriteCond %{HTTP_REFERER} !^https://(.*).yourmaindomain.com/.*$ [NC]
Don’t these lines need changes?
Below is .htaccess file I have now at nicholsonvineyards.com. No instructions on .htaccess file at nicholsonvineyards.mobi for RewriteEngine on or off.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/m/.*$
RewriteCond %{HTTP_ACCEPT} “text/vnd.wap.wml|application/vnd.wap.xhtml+xml” [NC,OR]
RewriteCond %{HTTP_USER_AGENT} “acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-” [NC,OR]
RewriteCond %{HTTP_USER_AGENT} “dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-” [NC,OR]
RewriteCond %{HTTP_USER_AGENT} “maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv” [NC,OR]
RewriteCond %{HTTP_USER_AGENT} “palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany” [NC,OR]
RewriteCond %{HTTP_USER_AGENT} “sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo” [NC,OR]
RewriteCond %{HTTP_USER_AGENT} “teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi” [NC,OR]
RewriteCond %{HTTP_USER_AGENT} “wapp|wapr|webc|winw|winw|xda|xda-” [NC,OR]
RewriteCond %{HTTP_USER_AGENT} “up.browser|up.link|windowssce|iemobile|mini|mmp” [NC,OR]
RewriteCond %{HTTP_USER_AGENT} “symbian|midp|wap|phone|pocket|mobile|pda|psp” [NC]
#————- The line below excludes the iPad
RewriteCond %{HTTP_USER_AGENT} !^.*iPad.*$
#————-
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC] #*SEE NOTE BELOW
RewriteCond %{HTTP_REFERER} !^https://(.*).yourmaindomain.com/.*$ [NC]
RewriteRule ^(.*)$ https://www.nicholsonvineyards.mobi [L,R=302]
Edmund–
Thanks for your response.
Got the redirect to work, but the back to full website link doesn’t work. Did some guesswork on lines 2 and 15 with the domains. I’m lost at this point. Here’s what I have right now at nicholsonvineyards.com .htaccess file (see below). Tried it with RewriteEngine Off in nicholsonvineyards.mobi .htaccess file but still didn’t work.
Maybe I’m not understanding something basic like .htaccess only works for parent directory and all subdirectories and not from one domain to another???
RewriteEngine On
RewriteCond %{REQUEST_URI} !^https://www.nicholsonvineyards.mobi.*$
RewriteCond %{HTTP_ACCEPT} “text/vnd.wap.wml|application/vnd.wap.xhtml+xml” [NC,OR]
RewriteCond %{HTTP_USER_AGENT} “acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-” [NC,OR]
RewriteCond %{HTTP_USER_AGENT} “dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-” [NC,OR]
RewriteCond %{HTTP_USER_AGENT} “maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv” [NC,OR]
RewriteCond %{HTTP_USER_AGENT} “palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany” [NC,OR]
RewriteCond %{HTTP_USER_AGENT} “sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo” [NC,OR]
RewriteCond %{HTTP_USER_AGENT} “teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi” [NC,OR]
RewriteCond %{HTTP_USER_AGENT} “wapp|wapr|webc|winw|winw|xda|xda-” [NC,OR]
RewriteCond %{HTTP_USER_AGENT} “up.browser|up.link|windowssce|iemobile|mini|mmp” [NC,OR]
RewriteCond %{HTTP_USER_AGENT} “symbian|midp|wap|phone|pocket|mobile|pda|psp” [NC]
RewriteCond %{HTTP_USER_AGENT} !^.*iPad.*$
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC] #*SEE NOTE BELOW
RewriteCond %{HTTP_REFERER} !^https://www.nicholsonvineyards.com/.*$ [NC]
RewriteRule ^(.*)$ https://www.nicholsonvineyards.mobi [L,R=302]
Hi Eric. Sorry you’re still having trouble. I do understand the frustration, I spent a couple of weeks looking for a solution that would actually work for me. It may be that the redirect loop will continue unless the domain is a subdomain, but I imagine that the code can be edited to allow for this.
One resource I used, which has an active community for help is https://ohryan.ca/blog/2011/01/21/modern-mobile-redirect-using-htaccess/ There code is a bit different from what I used, which was a mix of different things I had found online, but I would suggest starting here for answers. If they can’t help, just look for “htacess mobile redirect no loop”, and you will likely find what you need.
OK. Thanks for your help.