Mod Rewrite that creates safe domain to process AJAX requests

Hi there,

I am using an SEO URL module built into one of the popular e-commerce CMS systems but sadly my Apache code is not working. Every time I make Ajax requests from pages with the URL modified, I am getting CORS(cross domain-related) errors which tells me that my rewrite code is doing something very wrong.

All I want to do is be able to make HTTP requests via Ajax to relative file-paths as in:

index.php/myfillepath/myfile

I want to do this regardless of whether the page that I am on is modified by the SEO url module.

By this I mean that I want it to work whether a page is mydomain.com/seokeyword or mydomain.com/index/php?route=common/category

So in short can someone provide me with a safe mod rewrite please, I am mainly a JQuery/Php/HTML/CSS developer. My Apache code is patchy at best so I won’t even post it here as I know it is to blame for all of my problems.

All I want to do is rewrite mydomain.com/index.php to be mydomain.com without confusing the browser into thinking I am calling cross domain when using AJAX.

I would really appreciate it thanks

best regards

Silversurfer5150

SS,

IMHO, you should be using the direct link to the data you want instead of relying on redirections. Moreover, you need to have Options +MultiViews (an ugly Apache feature) to serve the file in the directory position in a URI (and have it parse the remainder of the path :nono:).

Finally, if you want help with mod_rewrite code, you really need to show it. :frowning:

Regards,

DK

Hi there,

Thanks for your reply,
By direct link, I presume you mean absolute path (in my http request), I am using a relative one due to the fact that certain browsers throw errors with absolute paths when using JQeury AJAX.

The only code I had is here:

# SEO URL Settings
RewriteEngine On
RewriteBase /your_sub_directory_name/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

It is from the opencart forum and AFAIK when you enable the SEO url feature in the admin section of the CMS, it checks the .htaccess, otherwise it is ignored. I really have no idea what the code does because it isn’t my area of expertise but I know that it isn’t working, I know nothing of any of the things you spoke of above, only that other people have got this feature working.

I should also explain that the AJAX I am using is a product search, a custom filter which is on the front page , so if this rewrite is performed incorrectly, AJAX returns a CORS error and the feature fails to work.

Can you help, even with my lack of knowledge on this end?

SS,

By direct link, I meant that you should not use a URL which must be interpreted and redirected, i.e., (relative link using) path/to/file. Your example used /index.php/yadda/yadda which is clearly incorrect (unless, of course, you have a subdirectory in your DocumentRoot named index.php, that it has a subdirectory named yadda with a file named yadda). If that’s the case, I doubt anyone can help you.

Now that I’m beyond that, it appears that your link is what’s causing the problem. So, are you using the link directly to the script which is supposed to return your AJAX information? Have you tested that it works?

Thanks for posting your .htaccess code (please use [noparse]

 ... 

[/noparse] wrappers - that’s what they’re there for.


# SEO URL Settings
RewriteEngine On
[COLOR="#FFA07A"]RewriteBase /your_sub_directory_name/[/COLOR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^[COLOR="#FF0000"]?[/COLOR]][COLOR="#FF0000"]*[/COLOR]) index.php?_route_=$1 [L,QSA]

Unfortunately, I’ve got to make some comments on it, too:

  1. Unless you’re hosting your CMS in a subdirectory, get rid of the RewriteBase BECAUSE it’s intended to undo a mod_alias redirection
  2. You’re excluding ? in the URI? It’s already excluded BECAUSE it’s a reserved character (which separates the end of the URI from the start of the query string).
  3. The * is the metacharacter for ZERO or more of the previous. Can your index.php handle a null input for the value of route?

Okay, those are mod_rewrite comments which you don’t understand but your OpenCart people should know if they’re generating that nonsense for you. In other words, that regex may also be a problem.

Please test your direct link to the script which AJAX is asking for data. Does it work when addressed directly? If so, then use that link!

Regards,

DK