简体   繁体   中英

.htaccess redirect everything except certain request

I am having an issue with my .htaccess file. It is redirecting everything through my index.php page which is what I want for the majority of requests.

BUT the only time I do not want to redirect is via an AJAX call.

It redirects any request through my index.php file like so:
RewriteRule /.* /index.php [NC,L]

The AJAX request url is:
http://myurl.dev/login/ajax/functions.php

With the directory structure:
/modules/login/ajax/functions.php

I am inexperienced with regex and RewriteRules, and have read / tried many variations with varying logic but cannot stop anything from /ajax/ to not redirect to the index page.

I have tried a RewriteCond before the Index RewriteRule to redirect to index unless /ajax/ but no luck.

Rewrite Cond %{REQUEST_URI} !(.*)/ajax
RewriteRule /.* /index.php [NC,L]

Also tried a seperate RewriteRule for the /ajax/ request:
RewriteRule ^(.*)/ajax/functions\\.php$ /modules/$1/ajax/functions.php [NC,L]

So nothing as worked so far, it either redirects to the index or hits a 500 server error .

Does anybody have any suggestions or links to help? Thanks.

Note : When I say redirect, I do not mean a full page refresh as I know that Apache wont do a full url refresh without the [R] flag.

-- Edit: Working file --

Here is my full .htaccess code:

Options +FollowSymLinks  
RewriteEngine on

# Intercept any requests to the core, and keep them being written to the core (so they don't go to index.php)
RewriteRule ^core/(.*)$ core/$1 [NC,L]

# The magic, stops any requests to a file for being redirected.
# needed to be under the /core/ redirect
RewriteCond %{SCRIPT_FILENAME} !-f

# Rewrite all requests to index.php.
RewriteRule /.* /index.php [NC,L]

# Some requests (without trailing slashes) can fall through the above rule. This bit catches those stragglers.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]

ErrorDocument 404 /404/

Use

RewriteCond %{SCRIPT_FILENAME} !-d //excludes existing directories
RewriteCond %{SCRIPT_FILENAME} !-f //excludes existing files

before any RewriteRule . This will exclude any directory or file which already exists, so the RewriteRule won't work on http://myurl.dev/login/ajax/functions.php , because it actually exists, but it will work on http://myurl.dev/someOtherNonExistantFile.php

That makes this your full .htaccess file code:

AuthType Basic
Options +FollowSymLinks  
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
# Intercept any requests to the core, and keep them being written to the core (so they don't go to index.php)
RewriteRule ^core/(.*)$ core/$1 [NC,L]

# Rewrite all requests to index.php.
RewriteRule /.* /index.php [NC,L]

# Some requests (without trailing slashes) can fall through the above rule. This bit catches those stragglers.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]

ErrorDocument 404 /404/

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