Problem with URL Rewrite

Well below is the URL rewrite code in my .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
RewriteRule ^/?ajax/(.*)$	ajax.php?$1 [NC,QSA,L]
RewriteCond %{REQUEST_URI} !\.(js|css|gif|jpg|png|ico)$ [NC]
RewriteRule ^(.*)$ index.php [QSA,L]

The idea is that if the url contains ‘ajax’, it will be redirected to the file ajax.php, which handles AJAX requests. Otherwise, it goes to index.php, except for some specific media files such as javascript, css and images. I have mod_rewrite enabled, so it will enforce pretty URL, or search-engine friendly url, such as /message/compose, rather than message_compose.php.

The issue is that, it only works in simple cases, once I start to have an action and some additional parameters, the rewrite rule for AJAX will fail. For instance, in the user profile page I have this URL: /profile/user/1, in which profile is the controller name, user is the action name and 1 is the parameter for userID. With this, the AJAX url rewrite rule stops working, and goes to index.php, throwing strange errors like ajax controller not found.

Can anyone help with this? Is it possible to force url rewrite to ajax.php so long as ‘ajax’ is found in the request uri? Or is it possible to prevent url rewrite if ‘ajax’ is in the url?

The rewriting process keeps going even if one of your rewrites already matched and changed the URL. So /ajax/foo first gets rewritten to ajax.php?foo, but then your next rewrite rule – which filters out some extensions but not “php” – rewrites to index.php.

Probably your not-a-file lines need to move. They should replace your line that does extension filtering.

# Rewrite www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]

# Rewrite /ajax/... to ajax.php?...
RewriteRule ^/?ajax/(.*)$   ajax.php?$1 [NC,QSA,L]

# All other non-files rewrite to index.php
# The not-a-file test will catch all css, js, images, etc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]

Thanks for your post, now I dont get errors with ajax request being directed to index controller anymore. However, I still have a strange problem when my url gets more complex. The following url will work: index, profile but index/, and profile/ will not work, of course adding additional parameters will not work either. It seems that if I add additional slashes/directory-separators, AJAX requests will fail. What might have caused this?

Do you have real directories called “index” or “profile”? Is there anything more to your htaccess? How does your PHP script ultimately try to use the URL?

Nope, I dont have such real directories. Index and Profile are controllers, and the subsequent parameters are actions and then controller parameters. For instance, this uri /profile/edit/1 has profile as controller, edit as action and 1 as user ID parameter. There is no directory called profile or edit.

Given the information at, I can’t see an obvious explanation. Can you give more information about the URLs that don’t work? What does “doesn’t work” mean? A 404 error? A PHP error?

Well the only URL that works is the URL that does not have more than one forward slashes, like /index, /profile and /message. So long as I start to have more complex URLs, such as /message/compose(which has 2 forward slashes), or /profile/edit/2(which has 3 forward slashes), it does not work anymore.

What I mean by ‘it does not work’ is that the AJAX request will not be made. For instance, on the registration page there is button that checks username asynchronously using AJAX if you press it. But right now, it wont work anymore because the URL to registration page is /auth/register, as of now AJAX requests only work properly if I have 1 parameter in the request URI, anything more than that will cause AJAX to stop working. Do you know what might possibly cause it?

Ahh. So you do get a page back, but the JavaScript on that page doesn’t behave the way you expect? My first thought is to check that your scripts are loading at all. Remember that relative paths are relative to the URL path, not your local file system path.

Yeah you make a good point. In the simpler URLs in which I only have one forward slash, I can see the javascript file cjax-5.8.min.js in firefox’s debugger. If I add more forward slashes in the url, it disappears. In the console tab of debugger, I get this:

Fatal error:  Uncaught  --> Smarty: Unable to load template file 'profile/cjax.tpl' --< 
  thrown in /home/public_html/bundle/smarty/sysplugins/smarty_internal_template.php on line 219

Its very strange, now I dont even know if the issue is with smarty, or URL rewrite. sigh And btw, this error happens before I made any AJAX request, but strangely it does not show up as PHP fatal error in a white page, and can only be found in debugger or error log.

I’ve got it to work now, thanks for your tip, it was indeed the problem with absolute vs relative url.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.