AJAX rewrite rule for directories - ajax

I'm trying to write a rewrite rule in addition to wordpress's :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I would not like everything to redirect to the main page. I have multiple sub-directories which need to be redirected too : http://www.example.com/sub1 and http://www.example.com/sub2. I'm doing this so my content can be loaded via Ajax.
I thought this would simply be
RewriteRule ^/(sub1|sub2)/(.*)$ http://www.example.com/$1/ [L]
Which I placed in the block here:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(sub1|sub2)/(.*)$ http://www.example.com/$1/ [L]
RewriteRule . /index.php [L]
</IfModule>
Perhaps with wordpress you must use wp_rewrite_rule? I figured I could mess with the .htaccess file, get my rule to work, then move it over to functions.
any help would be appreciated.
Thanks.
UPDATE:
I've also tried to set the .htacces file back to the wordpress default... and add this code to the functions.php file for my child theme:
function AJAX_rewrite_rule() {
add_rewrite_rule(
'^/(sub1|sub2)/(.*)$',
'http://www.example.com/$1',
'top' );
};
add_action( 'init', 'AJAX_rewrite_rule' );
Seems to slow the load of everything down, but all files not found are still redirected to the main page, not the subdirectory.
UPDATE #2
I think I was going the wrong direction, as add_rewrite_rule is only to add a rule to the structure already put in place by wordpress. This all works by interpreting URL's, and changing them to variables for a DB query run by index.
I'm pretty sure I need to use $wp_rewrite->non_wp_rules. If anyone has more of an idea, let me know.

Try this, you seem to have ine bad line there:
<IfModule mod_rewrite.c>
RewriteEngine On
# RewriteBase /
# RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f #Means DONT redirect if file exists.
RewriteCond %{REQUEST_FILENAME} !-d #Means DONT redirect if directory exists.
RewriteCond %{REQUEST_URI} !^/sub1/
RewriteCond %{REQUEST_URI} !^/sub2/
# RewriteRule ^/(sub1|sub2)/(.*)$ http://www.example.com/$1/ [L]
RewriteRule . /index.php [L]
</IfModule>
What happens is, every time you add a "RewriteRule" all the "RewriteCond" are applied to that rule, and then you start with a blank slate:
RewriteCond A
RewriteCond B
RewriteCond C
RewriteRule Whatever [L] #This applies A, B, and C
RewriteRule Another [L] #This applies no rules
Perhaps that will help.
Also, you may try adding your "RewriteRule ^/(sub1|sub2)/(.*)$ http://www.example.com/$1/ [L]" right after "RewriteEngine On" if that doesn't work…

Related

mod rewrite how do i add multiple rules with different combinations

i have been pulling my hair out for 3 days. i ve checked forums and tutorials but i just dont get it. i have a url of
http://localhost/gallery/2012/2
but i also need to get
http://localhost/gallery/2
this is my htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9-]+)/([0-9-]+)/?$ index.php?page=$1&date=$2&pagenum=$3 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9-]+)/?$ index.php?page=$1&date=$2 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?page=$1 [L]
</IfModule>
how would i go about adding another rule
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9-]+)/?$ index.php?page=$1&pagenum=$2 [L]

.htaccess file for magento in subdirectory

We are finding it difficult to remove the redirection of my /admin to /index.php/admin, which is not allowing us to access the admin panel, at every url that should start with /admin/ gets redirected to /index.php/admin/ and it displays No input file specified. and when i remove index.php from the url it works fine.
My .htaccess file looks like this
DirectoryIndex /new/magento-k/index.php
RewriteEngine on
RewriteBase /mysubdirectory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteCond %{REQUEST_URI} !^/admin.*
RewriteCond %{REQUEST_URI} ^/index.php/admin.*$
RewriteRule ^/index.php/admin(.*) /admin$1 [R]
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^/index.php/(admin|user)($|/) - [L]
Options -MultiViews
Try putting this into your .htaccess file
RewriteRule .* /index.php [L]
and remove the other index.php rule:
RewriteRule ^/index.php/(admin|user)($|/) - [L]
Also make sure to clear cache of Magento and your browser.

How do you remove index.php from a url using mod_write, while still allowing php to see the path with index.php in it?

For the past 3 days I have been playing around with Apache's mod_rewrite trying to get it to remove index.php from my url, while php still needs to see it in the path.
Essentially PHP needs to see this
http://example.com/index.php/Page/Param1/Param2
While the user needs to see this
http://example.com/Page/Param1/Param2
What I have right now is the following in an htaccess file
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
Which was taken from another page, and is close to what I need. However this seems to be cutting off everything after the http://example.com/ part. How can I get mod_rewrite to show the user one thing and have php see something else?
This is the modified code you can use in your .htaccess (under DOCUMENT_ROOT) to remove index.php from URI:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^index\.php)^(.+)$ /index.php/$1 [L,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php(/[^\s\?]+)? [NC]
RewriteRule ^ %1%2 [R=302,L]
Change R=302 to R=301 once you're satisfied that it is working fine for you.
This rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
Needs to look like this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /index\.php(.*)\ [NC]
RewriteRule ^ /%1 [R=301,L]
Also note that RewriteRule ^(.*)$ index.php?$1 [L,QSA] doesn't create a URI that looks like this /index.php/Page/Param1/Param2, it creates a query string that looks like this: /index.php?Page/Param1/Param2. Which isn't at all what you said PHP needs to see.

mod_rewrite but skip a directory

I'm attempting to avoid urls like this one:
images/
or
images/valid-filename.png
or
images/invalid-filename.png
from being rewritten
and I've checked other example questions like .htaccess mod_rewrite on root directory but need it to skip all other directories and .htaccess mod_rewrite exemptions and this one https://stackoverflow.com/questions/7209746/can-mod-rewrite-skip-a-folder but I've not managed to get the rules to achieve what I'm after.
Here is what I have right now:
# Enable URL Rewriting
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(scripts|styles|images)(/.*|$) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/robots.txt
RewriteCond %{REQUEST_URI} !=/favicon.ico
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This solved the problem
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RewriteCond %{REQUEST_URI} !images/.* [NC]
RewriteRule ^(.*)$ index.php [L,QSA]
</IfModule>
If I navigate to www.site.com/images/, I get redirected to www.site.com
If I navigate to www.site.com/images/invalid-filename.png, I get redirected to www.site.com
but www.site.com/images/valid-filename.png loads correctly.
Could someone help to explain how to achieve the rewrite behaviour described above?
Many thanks
Try adding this RewriteCond: RewriteCond %{REQUEST_URI} !images/.* [NC]
It will check whether the URI begins with images/ and if it does, the rule will not be met.

apache mod rewrite on a subdirectory

quick question about mod rewriting on a sub-directory when i already have a rewrite rule on the webroot to remove 'index.php'
my .htaccess looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
i have an API directory (/webroot/api/index.php) where i want to have a RESTful API (Restler) to serve requests into the application- so my question is..
to make RESTful requests i need to use the following URL format:
/webroot/api/index.php/user/get/ (to get all the users)
what i would like to achieve is something in the form:
/webroot/api/user/get/ (return a list of users)
which still retaining the base rewrite rules for the main website
/webroot/members/ (display a list of users in the main applications)
many thanks,
Justen
Your base rewrites look odd.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
This says that if we match index.php we are done (and no rewrite takes place). Is that what you want? If the request is exactly for /index.php with no trailing material, then it is left alone.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
To remove index.php you want something like:
RewriteRule ^index.php/(.*) $1 [L]
E.g. index.php/foo goes to foo (relative rewrite: then RewriteBase gets tacked on it turning into the /foo URL to feed back into the request pipeline.)

Resources