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.)
Related
I am using CMSMADESIMPLE for a website. It can generate search engine friendly URLs using a .htaccess file and mod_rewrite.
BUT it is possible to insert anything between the base url and the requested page. For example all these URLs will work:
www.example.com/aboutus
www.example.com/home/aboutus
www.example.com/any/rubbish/i/enter/aboutus
I have seen other sites using Wordpress where it is possible to enter the same in the address window, but it will redirect back to the lowest level i.e. www.example.com/aboutus. I would like to achieve the same thing but cannot master .htaccess to do it.
The relevant bits of the .htaccess file are:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>
I am assuming that this is something that can be achieved just using mod_rewrite and doesn't require changes to the PHP code within the CMS.
If that's not possible I would like to setup a permanent redirect from some URLs within the CMS which have been indexed by Google.
i.e From www.example.com/home/aboutus to www.example.com/aboutus
I tried adding a RewriteRule to .htaccess without success:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule home/aboutus aboutus [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>
Any help appreciated.
Look at the sample htaccess.txt file in the doc folder. There is a section about parent child.
# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
I have two Codeigniter sites, one sitting within the subdirectory of the other. I need some help modifying my htaccess file to remove index.php from both.
The first site, http://subdomain.domain.com is stored in /home/sites/subdomain/www/html/
The second, http://test.subdomain.domain.com lives in /home/sites/subdomain/www/html/app_staging/
This is my .htacess file from /home/sites/subdomain/www/html/:
RewriteEngine On
# Rewrite the main domain
RewriteCond %{HTTP_HOST} !test.subdomain.domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
# Rewrite the sub domain
RewriteCond %{HTTP_HOST} test.subdomain.domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ /app_staging/index.php?/$1 [L]
This is removing index.php from the from the first site, everything is ok there. On the second site, I can load the default controller form http://test.subdomain.domain.com, but subsequent pages return a 404 from the server. What are my next steps?
You should put each of two .htaccess files inside their own root CI folder.
Personally, I prefer restricting requested files and/or folders in order to prevent them from being treated by RewriteRule.
So, for the first file which is located at /home/sites/subdomain/www/html/:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico|public)
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
And for the other one is located at /home/sites/subdomain/www/html/app_staging/:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico|public)
RewriteRule ^(.*)$ /app_staging/index.php/$1 [L]
</IfModule>
Note: You must add css and other folders (like public) to RewriteCond if you don’t want them to be treated by RewriteRule.
As a side-note: You might want to prevent directory listing by using the following inside each .htaccess file:
<IfModule mod_autoindex.c>
Options -Indexes
</IfModule>
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…
We're currently using folders inside webroot to store images and videos which we're loading in views (using the usual html image helper) that require log in.
How can I prevent outside visitors from just doing a site.com/img/photos/1.jpg url and having access to the images?
From what I understand I can't really use media views to render an image inside a proper view, and I can't figure out if there's a solution through htaccess manipulation.
Which is the best practise for this?
Perhaps choosing to go with a non-webroot folder would be best (although that would make it harder in the file-storing part)?
As poncha suggested, I tried editing the main .htaccess file into this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} !localhost
RewriteCond %{REQUEST_URI} ^app/webroot/img/
RewriteRule .* / [L,F]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
But the rewrite base line seems to be forbidding access to the whole site, and without it there seems to be no change in img access.
Edit 2:
Editing the htaccess inside webroot:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
# this RewriteCond is needed to avoid rewrite loops
RewriteCond %{REQUEST_URI} !^/app/webroot/
RewriteRule (.*) app/webroot/$1 [L,R]
RewriteCond %{HTTP_REFERER} !127.0.0.1
RewriteCond %{REQUEST_URI} ^/app/webroot/img/
RewriteRule .* - [L,F]
</IfModule>
This checks if Referer http header is set to something containing your domain, and denies access to img/ folder if not.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} !site.com
RewriteCond %{REQUEST_URI} ^img/
RewriteRule .* / [L,F]
Note: it is easy enough to "break" this protection if someone wants to steal your content, however, it does prevent hotlinking without the need to produce some sort of script that would pass thorugh all the images/videos to check if access should be granted.
EDIT:
In case your website is not in /, You have two options:
Change RewriteBase to reflect the base uri of the site (eg RewriteBase /app/webroot/)
Change RewriteCond to reflect the path from / (eg RewriteCond ^app/webroot/img/)
The second option is preferred in your case because you have other rules there
EDIT2:
In your case, the whole set should look like this:
RewriteEngine on
RewriteBase /
# this RewriteCond is needed to avoid rewrite loops
RewriteCond %{REQUEST_URI} !^/app/webroot/
RewriteRule (.*) app/webroot/$1 [L,R]
RewriteCond %{HTTP_REFERER} !localhost
RewriteCond %{REQUEST_URI} ^/app/webroot/img/
RewriteRule .* - [L,F]
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.