mod_rewrite aliasing a subdirectory - mod-rewrite

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.

Related

.htaccess syntax code needing revision

1) i am trying to 301 all non www's to www's (including files)
e.g. /subdirectory/ + /subdirectory/1.jpg (all possibilities with the www.)
2) i am trying to exclude ONLY but ALL .html and .php files from showing and would like only the non trailing version to be indexed, the rest to be 301's to my domain
e.g. /example.html or .php >> /example
e.g. /example.html or .php >> to not work + to not be indexed
e.g. /example to ONLY work and to ONLY be indexed (to avoid duplicate content)
3) i am trying to 301 all dead links and 404's to my domain
e.g. /deadlink or /deadlink.pdf >> 301'd to my domain, example.com
Here is the code i currently have, however i am not sure if it's 100% proper.
Can someone please reply with a validated syntax for these 3 tasks? Thanks.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]
OK, you've got three parts you're trying for here, and I'll address them all separately.
Before we begin, you only need to have one
RewriteEngine on
It only needs to be turned on once.
You should also set:
Options -MultiViews
as MultiViews can otherwise cause some weird issues here.
Redirect to WWW
Your current rules should work, but you can simplify them a little using the != prefix on RewriteCond:
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Removing .php and .html suffixes
You need two separate sets of rules here: one to remove suffixes from URLs, and another to readd them internally.
Let's do the second half first, since that's easier:
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
With this in place, though, removing suffixes is a little tricky without conflicting with the rule that goes ahead and tries to put them back on. The best approach I was able to come up with was:
RewriteCond %{THE_REQUEST} \.(html|php)
RewriteRule ^(.*)\.(html|php)$ $1 [R=301,L,NC]
But I'm open to suggestions.
301ing all dead links
Don't do that. If a link doesn't exist, it should return a page with a 404 status. If you want to redirect visitors to your home page from dead links, put a link in your 404 page.

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…

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.)

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

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.

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]

Resources