Hi,

I am trying to write a .htaccess script that can redirect any URL on my site (whether it exists or not, including sub-domains) to the index page of that site.

For example:
example.com/pages/page.htm ---> example.com
---> example.com
---> example.com

I've tried a few things with mod_rewrite but I can't seem to get existing urls, non-existing urls and sub domain urls to all direct to the index page.

Thanks,
Martin

Dani AI

Generated

Good to note: ’s rule internally rewrites to index; the browser URL will not change. If you want every request (existing or not) to land visibly on one homepage, use external redirects and guard against loops. Put the rules in the vhost (or shared DocumentRoot) that actually serves each subdomain so those requests are caught. The [R] flag triggers an external redirect; pair it with [L] to stop further processing. (httpd.apache.org)

Drop this in the top-level .htaccess of example.com, replacing example.com with your real host. Use https in the target only if the site supports it.

RewriteEngine On

# 1) Send any non-canonical host (including subdomains) to the apex
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^ https://example.com/ [R=301,L]

# 2) On the apex, collapse any path to "/"
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^ / [R=301,L,QSD]

The first rule canonicalizes the host. The second turns anything like /foo/bar into /. Add QSD if you want to discard any incoming query string; omit it if you prefer to keep queries. (httpd.apache.org)

For ’s www case (keep the path, just change the host):

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

This is the canonical-host redirect pattern shown in the Apache recipes; use it when you want one public hostname for all content. (httpd.apache.org)

Tip: test with a temporary 302 first, then switch to 301 once you confirm no loops or asset breakage. The R flag defaults to 302 if you omit the code. (httpd.apache.org)

Recommended Answers

All 5 Replies

Hi,

I am trying to write a .htaccess script that can redirect any URL on my site (whether it exists or not, including sub-domains) to the index page of that site.

For example:
example.com/pages/page.htm ---> example.com
---> example.com
---> example.com

I've tried a few things with mod_rewrite but I can't seem to get existing urls, non-existing urls and sub domain urls to all direct to the index page.

Thanks,
Martin

try this:

RewriteEngine On
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule .* /index.php

try changing index.php to index.html, or whatever the index file is you want to see. i think that will work, though I havnt tested it.

Thanks Kyle,

Sorry for the delayed reply.

That worked perfectly :)

Thanks again,
Martin

This worked for me:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.*)$ index.php/$1 [QSA,L]
RewriteEngine on
RewriteRule ^(.*)$ index.php?url=$1 [L]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.