Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

mod_rewrite www Redirect: Exclude app-ads.txt?

Learn how to redirect to www using mod_rewrite while excluding app-ads.txt for Google AdMob compliance.
mod_rewrite www redirect exception for app-ads.txt with Apache code example and AdMob compliance warning mod_rewrite www redirect exception for app-ads.txt with Apache code example and AdMob compliance warning
  • ⚠️ Redirecting app-ads.txt breaks AdMob checks and stops ad money.
  • 🧳 Google needs app-ads.txt on your main domain, not www subdomain.
  • 🚀 Your mod_rewrite rules must skip app-ads.txt to prevent problems.
  • 🛠 Using VirtualHost settings instead of .htaccess works better.
  • 👀 Testing helps make sure redirects don't mess up SEO or ad platforms.

Website owners often send users from the non-www version of their site to the www version. This is a standard practice for SEO and consistency. But if you make money from mobile apps with Google AdMob, and you don't exclude the app-ads.txt file from this redirect, it can stop ad checks. This can make you lose a lot of money. This guide explains how to use mod_rewrite in Apache to set up a www redirect htaccess the right way. We will focus on making sure your app-ads.txt file stays accessible.


mod_rewrite and .htaccess: How They Work

What is mod_rewrite?

The Apache mod_rewrite module lets you make many strong changes to URLs. It can direct traffic to the right place, make sure HTTPS is used, or standardize domain names. This module is key to many current server setups. It works by following rules. These rules are set either in your main Apache configuration or in .htaccess files. These are small configuration files that sit in specific directories on your web server.

How .htaccess Files Work with mod_rewrite

The .htaccess file lets you change settings for a specific directory without changing the main Apache settings. This is very helpful in shared hosting where you can't get to the main server settings.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Rules in .htaccess:

  • Are checked from top to bottom.
  • Only start working after Apache finds the requested directory.
  • Can accidentally cancel each other out if not put in order carefully.

This means you need to pay close attention to the order of rules when you do a mod_rewrite redirect with .htaccess. This is even more important when other files, like app-ads.txt, need to skip these redirects.

Why Redirect to www in the First Place?

Sending all traffic to either the www or the non-www version of your domain is called canonicalization. This method is used to:

  • Get rid of duplicate content for search engines.
  • Combine SEO numbers, like link value.
  • Bring together analytics data under one name.
  • Make branding and marketing links consistent.

SEO Implications

Google and other search engines see www.example.com and example.com as different things unless you clearly tell them they're the same. If link strength, domain scores, or social shares are split between both versions, this can hurt your SEO, how visible you are, and your chance to rank high.

🧠 Moz says canonicalization is very important for keeping your domain name consistent. Not doing it could lessen search engine trust and authority (Moz, 2022).

Technical Benefits of Using www

  • For some server setups, using the www subdomain makes DNS management easier.
  • Cookies can be set up in different ways. For instance, cookies set on www.example.com can be kept separate from subdomains like api.example.com.

Redirecting to a www version is not just about looks or branding. It makes infrastructure, security, and following rules simpler.


app-ads.txt: A File You Can’t Afford to Redirect

What is app-ads.txt?

The app-ads.txt file is part of the IAB-approved Authorized Digital Sellers initiative. This makes sure app makers say who can sell their ad space. It is very important for stopping mobile ad fraud.

For example, a line in your app-ads.txt file might look like:

google.com, pub-0000000000000000, DIRECT, f08c47fec0942fa0

This confirms that the listed domain is allowed to sell ads on your apps through Google's AdMob or other platforms.

Why app-ads.txt Must Be on the Root Domain

Google and other ad services clearly state they need app-ads.txt to be on the main part of your domain, usually at:

https://example.com/app-ads.txt

Not:

https://www.example.com/app-ads.txt

Nor:

https://example.com/subfolder/app-ads.txt

Any redirect, even a 301 redirect, from this path can stop the check from working. And AdMob ads won't show up at all. Google's 2023 documentation says if the file is not found at this exact path, it stops apps from making money.

Real Monetary Risks

When the AdMob crawler can't find or check the app-ads.txt file, it thinks something is set up wrong or that there might be ad fraud. Then it stops showing ads until the check is fixed. This can take hours or even days. This means it directly affects how much ad money you make each day.


How to Write an Effective www Redirect in mod_rewrite While Excluding app-ads.txt

To do a mod_rewrite redirect correctly, you must watch out for exceptions like app-ads.txt. Use this .htaccess setting:

RewriteEngine On

# Allow app-ads.txt to bypass redirect
RewriteCond %{REQUEST_URI} !^/app-ads\.txt$ [NC]

# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]

Explanation of Key Elements

  • RewriteEngine On: Turns on mod_rewrite rules for this directory.
  • RewriteCond %{REQUEST_URI} !^/app-ads\.txt$: Checks if the requested address is not app-ads.txt.
  • [NC]: Makes the check not care about capital letters.
  • %{HTTP_HOST} !^www\.: Makes sure you only redirect requests that don't already have www.
  • R=301: Sends a permanent redirect (301), which helps search engines.
  • [L]: Stops any more rules from running once this one is applied.

Make sure this block is near the top of your .htaccess file so it works best and other rules don't get in the way.


Where to Place app-ads.txt on Your Server

The right place for your app-ads.txt file changes based on your hosting. But the main point is still the same: it must be on your main domain.

Typical Path on Shared Hosts

  • /public_html/app-ads.txt
  • /public_html/assets/app-ads.txt
  • /public_html/www/app-ads.txt

And check file permissions to make sure anyone can read it, most often with a permission like 644.

You can use this command through FTP or SSH to check it:

ls -la | grep app-ads.txt

How to Test That Your Setup Works

After putting your rewrite rules and app-ads.txt file in place, you need to test two things:

1. app-ads.txt Accessibility (No Redirect)

curl -I http://example.com/app-ads.txt

Look for a 200 OK response, not a 301 or 302. This means the file is sent directly.

2. Redirect Non-www Traffic to www

curl -I http://example.com/

This should display:

HTTP/1.1 301 Moved Permanently
Location: https://www.example.com/

3. Google’s Tools

Use the AdMob validation tool to check your app-ads.txt again. If it's good, your ads should start showing again with no problems.

4. Server Logs

Check Apache access/error logs to find unusual situations or strange traffic.


Commonly Made Mistakes You Must Avoid

  • Redirecting app-ads.txt by mistake – Stops ads from being checked
  • RewriteCond rules in the wrong order – Causes things to not work right
  • Wrong regex patterns – Let app-ads.txt be redirected
  • Wrong file location or permissions
  • Using JavaScript redirects – Not good for SEO and doesn't work for bots

Even experienced developers can miss these simple errors, especially if many people work on the server rules.


Performance Tip: Use VirtualHost Config If You Can

If you're on a dedicated server or a VPS, skip using .htaccess entirely for better performance with Apache.

Sample VirtualHost Configuration

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/app-ads\.txt$ [NC]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
</VirtualHost>

Benefits:

  • Stops .htaccess from being read for each request.
  • Simpler to manage everywhere.
  • Good for SSL, many domains, and special situations.

What to Do If AdMob Stops Working

If you set up redirects wrong and AdMob ads stop showing:

  1. Check if it's reachable: Go to https://example.com/app-ads.txt yourself or with curl.
  2. Fix Redirect Rules: Change .htaccess or VirtualHost to skip the file.
  3. Re-upload or replace the file: Make sure permissions and format are right.
  4. Check again in AdMob Console: Use the AdMob validation tool.
  5. Wait: AdMob might take a while to start showing ads again.

Write down changes for your team so it doesn't happen again when someone next edits your .htaccess.


Complete Checklist: www Redirect Without Breaking app-ads.txt

Task Status
app-ads.txt is on the main domain (https://example.com/app-ads.txt)
Requests for app-ads.txt are not redirected to www.
All other non-www requests send a permanent redirect to https://www.example.com/
Web server uses 301, not JS redirects
Check app-ads.txt with Google tools often
Test on a test server before going live
Write down all changes to .htaccess or VirtualHost

Integrate Redirect Safety into Your DevOps Workflow

To stop problems later:

  • 🔁 Set up automatic tests in your CI/CD pipeline using curl.
  • 📘 Keep a rewrite config README that your team can access.
  • 🔍 Have SEO and backend teams help test redirects.
  • 📡 Set up alerts for 404/301 errors with UptimeRobot, Pingdom, or similar tools.

Redirect rules are a small part of your code, but they are key between the user and your app. Test them well and have teams work together on them.


Sample .htaccess Snippet You Can Copy Now

Here’s your ready-to-use, good for SEO, and AdMob-friendly redirect rule:

RewriteEngine On

# Don't redirect app-ads.txt
RewriteCond %{REQUEST_URI} !^/app-ads\.txt$ [NC]

# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]

Customize if needed for SSL or domains in other countries—but always test completely.

Check the Apache mod_rewrite documentation to find more complex ways to send traffic.


Citations:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading