.htaccess重定向除特定请求之外的所有内容

时间:2012-11-15 17:26:27

标签: php jquery ajax regex .htaccess

我的.htaccess文件存在问题。它通过我的index.php页面重定向所有内容,这是我想要的大多数请求。

但我唯一不想重定向的是通过AJAX调用。

它通过我的index.php文件重定向任何请求,如下所示:
RewriteRule /.* /index.php [NC,L]

AJAX请求网址是:
http://myurl.dev/login/ajax/functions.php

目录结构:
/modules/login/ajax/functions.php

我对正则表达式和RewriteRules缺乏经验,并且已阅读/尝试了多种不同逻辑的变体,但无法阻止 / ajax / 中的任何内容重定向到索引页。

我在Index RewriteRule之前尝试过RewriteCond重定向到索引,除非 / ajax / 但没有运气。

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

还为/ ajax / request尝试了单独的RewriteRule:
RewriteRule ^(.*)/ajax/functions\.php$ /modules/$1/ajax/functions.php [NC,L]

到目前为止没有任何工作,它重定向到index或点击500 server error

有人有任何建议或链接可以提供帮助吗?感谢。

注意:当我说重定向时,我并不是指整页刷新,因为我知道Apache在没有 [R] 标志的情况下不会进行完整的网址刷新。

- 编辑:工作文件 -

这是我的完整.htaccess代码:

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/

1 个答案:

答案 0 :(得分:1)

使用

RewriteCond %{SCRIPT_FILENAME} !-d //excludes existing directories
RewriteCond %{SCRIPT_FILENAME} !-f //excludes existing files
在任何RewriteRule之前

。这将排除已存在的任何目录或文件,因此RewriteRule无法在http://myurl.dev/login/ajax/functions.php上运行,因为它实际存在,但它可以在http://myurl.dev/someOtherNonExistantFile.php上运行

这使你成为完整的.htaccess文件代码:

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/