Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My site currently forces all non-www. pages to use www.

Ultimately, I am able to handle all unique subdomains and parse correctly but I am trying to achieve the following (ideally with mod_rewrite):

When a consumer visits www.example.com/john4, the server processes that request as:

Our requirements are:

  • The URL should continue to show www.example.com/john4 even though it was redirected to www.example.com/index.php?Agent=john4

  • If a file (of any extension OR a directory) exists with the name, the entire process stops an it tries to pull that file instead:

    For example: www.example.com/file would pull up (www.example.com/file.php if file.php existed on the server. www.example.com/pages would go to www.example.com/pages/index.php if the pages directory exists).

share|improve this question

migrated from serverfault.com Feb 22 '11 at 23:26

This question came from our site for system and network administrators.

If you turn on MultiViews, you can do this, as it will search for request-uri.* first before running the RewriteRule.

Options +MultiViews
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?Agent=$1 [L]
share|improve this answer

Try this (untested)

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?Agent=$1 [L]
share|improve this answer
RewriteBase in this case is not necessary. The code continues to fail (although from looking through your suggestion it seems it should work. – JM4 Feb 23 '11 at 16:30
@JM4 "continues to fail" - Fail as in error? Or not the desired result? There could also be a conflict with existing code, since you mention a canonical www redirect and handling of subdomains - presumably in .htaccess also? This new code should probably go after any existing directives. – w3dk Dec 10 '15 at 23:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.