.htaccess "Ignore all rules if link starts with..." - ajax

In my root folder I have installed wordpress and there is also my submenu.php that can not be loaded with ajax if I use rules for /%postname%/ (in default )
So this is what WP gave me
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
# END WordPress
What do I need to add so that calling
$('#submenu').load('submenu.php?cat=4');
works again?

This is not the way you should be performing AJAX within WordPress.
I suggest you read up on Using AJAX within WordPress from the codex.

I am not really good with htaccess, but this
RewriteRule !^media/ index.php [L]
Will redirect everything except media/* to index, so something like this should work
RewriteRule !^yourscript.php index.php [L]
Note: I agree with Jason there, using it without htaccess is better.

Related

AJAX rewrite rule for directories

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…

Codeigniter - removing index.php

I downloaded MyClientBase application which is based on code igniter. After the installation, I looked at the MyClientBase's website but the index.php is still there by default.
I tried to remove the index.php using the tips in codeigniter's user guide but it doesn't work.
Is there any solution out there for this issue? Thank you
I use the following .htaccess file to remove index.php. I'm not sure which one the user guide specifies, but I found this to be working perfectly:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|(.*)\.swf|images|robots\.txt|css|docs|cache)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 /index.php
</IfModule>
Also remove 'index.php' from your config.php file. Good luck!
i don't know if it will help but, i use this to remove my index.php from codeigniter url, create a file named .htaccess and put it to your codeigniter folder.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]

mod_rewrite aliasing a subdirectory

I'm struggling with mod_rewrite as always. We have a number of client portals running through WordPress multisite, all accessed through a subdirectory: portal.
So for example: http://www.mydomain.com/portal/clientA/
I'd like to be able to get there just by typing http://www.mydomain.com/clientA/ and it would redirect me to http://www.mydomain.com/portal/clientA/
Here's what I have so far, and it's not producing any rewrite that I can tell:
RewriteCond %{REQUEST_URI} /portal/
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [S=1]
RewriteRule /clientA(/?) /portal/clientA/
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The second part I can't touch because WordPress needs it. My pattern is also trying to anticipate someone not putting in the trailing slash, hence the (/?)
EDIT: I should also note that I don't want to create a more general rule - I'm comfortable having to add a rewrite rule for each new client and increasing the S=x number each time.
EDIT (Aug 11), So after a little more puttering this is what my .htaccess is at:
RewriteEngine On
RewriteRule ^clientA(/?) /portal/clientA/ [R]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Needless to say it doesn't work. However, the first part works IF I delete the entire WordPress section. I need them BOTH to work simultaneously. WHAT is it about the WordPress piece that is causing the failure of the first section? I suppose it's the combination of RewriteBase and the very last rule which aliases anything else to /index.php, which frankly is a bit of a bummer. In fact I don't truly understand how that rule could even work in a multisite context, and yet it seems to.
FINAL SOLUTION
thanks to LazyOne for the correct answer! For others' reference, the final solution I used was:
RewriteEngine On
RewriteRule ^clientA(/.+)? /portal/clientA$1 [R,L]
RewriteRule ^clientB(/.+)? /portal/clientB$1 [R,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
As simple as this:
RewriteCond %{REQUEST_URI} !^/portal/
RewriteRule (.*) /portal/$1 [L]
It will rewrite (internal redirect) all requests into /portal/ folder (e.g. /clientA/something => /portal/clientA/something).
If you need to do it for some clients only (or, better say, only specific folders that are clients while still having some general/common folders as is), you can use this rule for each client:
RewriteRule ^clientA(.*) /portal/clientA$1 [L]
So that .htaccess will look like this:
RewriteRule ^clientA(.*) /portal/clientA$1 [L]
RewriteRule ^clientB(.*) /portal/clientB$1 [L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I posted part of this as a comment, but figured it might be more clear for people who land here to do this as an answer. To the OPs point, he found a way to do this using the [R] (redirect) line, but this eliminates the subdirectory URL structure you created that would be preferable in most URL Rewrites. So, the answer previously posted is right, I'm not contesting that, but depending on your implementation, you may still get WordPress 404 errors. Here is a solution to my situation, which I think might be more common.
In my case, I needed a URL structure like this:
http://mysite.com/p/profile_name/
Each user who comes to register can create his/her own profile on the fly, and aside from a few modifications to the content, for the most part all of the WordPress content at the root is what will be displayed. Essentially, I need this:
http://mysite.com/p/profile_name/(.*)
To be rewritten to this:
http://mysite.com/$1
This is the .htaccess code posted in the other answer that WILL handle that rule correctly:
RewriteRule ^p/([-a-zA-Z0-9_]+)(/.*) $2 [L]
The problem with this is that WordPress does not care about your rewrite in terms of understanding what $2 is because WordPress uses $_SERVER['REQUEST_URI'], which no matter what you rewrite to, it's always what's in the user's browser window. The OP found a way to get around this by using the [R] option, but that causes you to lose your URL:
RewriteRule ^p/([-a-zA-Z0-9_]+)(/.*) $2 [R,L]
Redirects:
http://mysite.com/p/profile/(.*)
To:
http://mysite.com/$1
But, the user loses his unique URL this way. At best you can add a query string to at least retain the data, but then you lose the point of the pretty URLs to begin with.
I did come up with a solution; however, it involves hacking the WordPress include files :( If someone has a better way, please update. Here we go:
SOLUTION
I set my .htaccess file equal to this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
#My addition:
RewriteRule ^p/([-a-zA-Z0-9_]+)(/.*) $2 [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Then, I knew WordPress used the REQUEST_URI variable, so I did a recursive search, and located this line of code in /wp-includes/class-wp.php (line 147 v3.4.2):
$req_uri = $_SERVER['REQUEST_URI'];
I changed it to this:
$req_uri = preg_replace(#'/^\/?p\/([-a-zA-Z0-9_]+)\//', '', $_SERVER['REQUEST_URI']);
Which basically just tricks WordPress by filtering out the profile stuff at the beginning of the URI.
Lastly, I also needed a solution for the links within the site. For that, I added this filter:
NOTE: Some of these regexes might be a little bonkers; I was filtering out the site specific stuff, so please use this as a concept and not a copy/paste.
function mysite_wp_make_link_relative( $link ) {
$sBaseUrl = (preg_match('/^\/(p\/[-a-zA-Z0-9_]+\/)(.*)/', $_SERVER['REQUEST_URI'], $matches)) ? $matches[1] : '';
return preg_replace( '|https?://[^/]+/(.*)|i', '/' . $sBaseUrl . '$1', $link );
}
function rw_relative_urls() {
$filters = array(
'page_link', // Page link
'home_url',
'site_url',
'get_site_url',
'home_link',
);
foreach ( $filters as $filter ) {
add_filter( $filter, 'mysite_wp_make_link_relative' );
}
}
Some of those filters might not be relevant; I'm pretty sure page_link and home_url are the only important ones. Anyway, you need that code for your internal linking to work.
I hope that helps and if anyone has any comments suggestions for improving this, I would greatly appreciate it.

404 is given when trying to access existing controller in CodeIgniter

I am working on a fairly new install of CodeIgniter 2.0.
I have a htaccess-file containg this code
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Going to the URL http://www.estiem.no/ESTIEM/CI/ works,
but http://www.estiem.no/ESTIEM/CI/site/index does not. It gives a 404. The controller Site exists, and contains the method 'index'
Any ideas what might be wrong?
You need to remove / from index.php
RewriteRule ^(.*)$ index.php/$1 [L]
It is a htaccess problem, back when everyone passed that code around we found that a lot of hosts didn't like it and you could hack it with a ? or this or that until it ended up looking like a jigsaw puzzle (the condition that is) but using a brilliant htaccess (not sure who the first was but like 500 have taken credit) you will never have to change it again (well not never but it will work 99% of the time un edited):
# Customized error messages.
ErrorDocument 404 /index.php
# Set the default handler.
DirectoryIndex index.php
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
Trust me I have had the same issues and spend hours on google when i should have come here or even the CI wiki (since that is the first place I seen it), you would not believe how many issues are solved in CI with just that bit of htaccess. they really should add it to the repo.
I just noticed that since you have your CI install in a subfolder, you need to account for that in the .htaccess rewrite:
/ESTIEM/CI/
So your .htaccess will look like:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /ESTIEM/CI/index.php/$1 [L]
That should fix it (sorry late edit, due to #BrianOrtiz 's comment)
Try to access the controller with: localhost/site/index.php/controller
If that does not work maybe because the CI is case sensitive
localhost/site/controller == c:...\site\controller.php
localhost/SITE/controller == c:...\SITE\controller.php
else
<?php
controller Mycontroller extends Controller {
}
?>
will be named mycontroller.php in the controllers folder
If all else fails replace your htaccess by this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]

Request exceeded the limit of 10 internal redirects due to probable configuration error.?

i have a simple rewrite
RewriteRule ^.*$ addnew/$0
however i get the
Request exceeded the limit of 10 internal redirects due to probable configuration error.
I am trying to rewrite
www.mysite.com/anycharacter
into
www.mysite.com/addnew/anycharacter
Try the folllowing solution, it works for me.I hope this will work for you too.
BEFORE DOING ANYTHING - BACKUP YOUR DB!
Now, go into wordpress > settings > permalinks
Set it to one of the pretty permalink settings like //year/month/'
Open a text editor on your PC/Mac and open the .htaccess file you downloaded from your
webserver
Find this piece of code
# BEGIN Wordpress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Replace it with this piece of code, courtesy of Scott Yang:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php/$1 [L,QSA]
</IfModule>
Upload this code into the main domain root directory on your server and you're done.
Everything should work
As RC already said, .* will also match addnew/. And since the L flag causes a reinjection of the rewritten rule, you will get an infinite recursion.
So adjust the rule so it doesn’t match your addnew/:
RewriteRule !^addnew/ addnew%{REQUEST_URI}
.*matches addnew/. Try with:
RewriteRule ^[^/]*$ addnew/$0

Resources