SancteIra SancteIra - 2 months ago 6
Apache Configuration Question

How to remove part of URI/ Redirecting a non-existent directory to an existing directory without changing the URI

I have 7 URLs that are written:

http://www.example.com/TERMS/file

what I want is:

http://www.example.com/file

Problems are:


  • files aren't in my main directory so its a non-existing path, causing error:404 when I try rewriting it normally with mod_rewrite

  • I have file extensions turned off using the following:

    > RewriteCond %{REQUEST_FILENAME} !-d RewriteCond
    > %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php RewriteCond
    > %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
    > RewriteRule ^(([^/]+/)*[^.]+)\.php /$1 [R=301,L]



which I believe is causing some conflict with the rewrites that I try doing.

If it helps, I also have mod_rewrite also forcing www on the address

I have tried looking for solutions on this site and have tried over 10 different methods, but none have worked for me.

and no, neither

> RewriteRule ^(.*)/TERMS/(.*) $1/$2


nor the

> (.+)


variation work for me.

Answer

You can use this rule after other rules in your DOCUMENT_ROOT/.htaccess file:

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!TERMS/).+)$ /TERMS/$1 [L,NC]