简体   繁体   中英

override apache rewrite rules with htaccess

working on a server that hosts multiple domains. Somewhere in apache config is a rewrite rule that is set for ALL domains.

What happens is if a user goes to example.com/foo they are supposed to get redirected to example.com/foo_bar

However, in one domain, I want to override this behavior so that the url stays at example.com/foo and does not redirect. I've been searching and trying various rules and conditions to no avail. I don't have access to the apache config, but I am using .htaccess for some rewrite rules on this domain.

here's my rewrite rules in .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine on

    # this isn't working
    # RewriteRule ^(foo)($|/) - [L]

    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?/$1

    # this didn't work either
    # RewriteRule ^/foo index.php?/$1 [L]
</IfModule>

Try redirecting /foo_bar back to /foo .

When Apache processes the rewrite rules it runs through them multiple times, which you can see when you turn on debugging . I think what's happening in your case is that you're trying to match the URI in the first pass, but Apache has already modified it to /foo_bar .

Also, as a matter of debugging, you should try to recreate the problem in an environment you control. Ask your sysadmin for a copy of the global configuration and mirror the set up you're constrained to.

You can create exception for one domain:

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} !^(?:www\.)?domain\.com$ [NC]
RewriteRule ^foo(/.*)?$ /foo_bar$1 [L,R=302]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM