2

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:
www.example.com?Agent=john4

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).

2 Answers 2

1

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]
1

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]
</IfModule>
2
  • 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, 2011 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.
    – MrWhite
    Dec 10, 2015 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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