I have the following in my htaccess file. What I'm trying to do is to make
domain.com/index.php/view/whatever accessable via domain.com/whatever
and also redirect from non www to www.
This works for all urls that have index.php/view in them but now other URLs that don't have index.php/view in them are breaking not working. Ex: domain.com/index.php/site/pages no longer works since it doesn't have index.php/view in it.
I want htaccess to only affect those URLs that have index.php/view in them and not anything else. What do I need to do to fix that?
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/view/$1 [L]
RewriteCond %{http_host} ^domain.com [nc]
RewriteRule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
UPDATE. To narrow things down, How can i have both rules like so. i need them both
RewriteRule ^(.*)$ /index.php/view/$1
RewriteRule ^(.*)$ /index.php/site/$1
So if a request is made to something like http://domain.com/index.php/site/pages, you want it to pass through untouched, but if it's something like http://domain.com/whatever, you want it to get rewritten?
The first rule you have there matches everything (except files and directories). You probably want to narrow the RewriteRule with something like:
RewriteRule !^index.php /index.php/view/$1 [L]
HTH
Neal
Related
I know that there are a bunch of answers for this question already on this forum and over the internet. But everything I tried is not working for me and I have no clue why. So that's why I'm asking this question.
I'm running a website with ExpressionEngine CMS. To remove index.php from the URL there is a small .htaccess rule which mentioned below and working fine.
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Everything running fine with this code in the htaccess file. But now I want to force www. in the URL because some AJAX scripts are not working when the URL is without www.. So I found some threads on this forum:
.htaccess - how to force "www." in a generic way?
https://expressionengine.stackexchange.com/questions/9853/site-doesnt-work-with-www-at-the-start-of-the-address/9864#9864
https in htaccess and order of rules (using Expression Engine)
The last has the most clear solution in my opinion so I added this to my .htaccess file.
# Add www.
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
My .htaccess file now looks like this:
RewriteEngine on
# Add www.
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# remove index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
This however give's me a ERR_TOO_MANY_REDIRECTS error.
I tried all other kind of methods but I keep getting this error and I don't know why. Everything I try to force www. through htaccess doesn't work or gives me this error.
What is going on? Is it possible that DNS settings have to do something with this?
You need to change it on Plesk control panel, you access the domain in question and change the non-www to www or set it to none.
The option you look for is located at subscription > Web Hosting Settings > Preferred Domain.
In case you set it to none then you can use the .htaccess rule.
I'm having a hard time having this to work..
I have installed YOURLS wich is a PHP script to shorten urls.
In order to work, it needs to have this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /yourls-loader.php [L]
No problem here.
But I also want to use a directory for image hosting that has nothing to do with the PHP script.
It would check if the requested url ends with .jpg|.jpeg|.gif|.png and RewriteRule would redirect to /imgshare/$1
I've tried the code below but I get a server error when going to mysite.com/img.jpg but not for the url redirection "mysite.com/y4Jd":
RewriteCond %{REQUEST_URI} !(\.jpg|\.jpeg|\.gif|\.png)$ [NC]
RewriteRule ^(.*)$ /yourls-loader.php [L]
RewriteCond %{REQUEST_URI} (\.jpg|\.jpeg|\.gif|\.png)$ [NC]
RewriteRule ^(.*\.(jpeg|jpg|png|gif))$ /imgshare/$1 [L]
This is not the issue, but as a note, your second RewriteCond already matches files with image endings, so there's no need to repeat that match in the RewriteRule. Alternately, there's no need for the RewriteCond since it's redundant to the RewriteRule.
The real issue, however, may be that you have an extra slash in the final rule. $1 will contain the leading slash matched from the original URL so your rule is currently adding two slashes between imgshare and the file name. I would implement the rule like this:
RewriteCond %{REQUEST_URI} \.(jpg|jpeg|gif|png)$ [NC]
RewriteRule ^(.*)$ /imgshare$1 [L]
I am taking over a site and I'm trying desperately to get our affiliate sites to stay at their rewrite paths. Instead, they all forward to the home page. I want 404s obviously to redirect and I know I am missing something here, but I've tried everything I can think of and I obviously need another pair of brains here. This is how it was written and I'm not sure how to edit it for what I need. How do I stop the affiliate pages from then forwarding to the index?? Seems like every edit I made breaks things!
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
RewriteRule ^aff/(.*)$ /index.php/aff/?aff=$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [R=301,NC]
ErrorDocument 404 /redirect/404_redirect.php
Redirect 301 /redirect/404_redirect.php http://privatemoneyexchange.org
Hate to ask this question, but I've been banging my head on the desk for a while and can't seem to get it. I'm using ExpressionEngine, and I'm using mod_rewrite to remove index.php from all of my URLs. That works fine. Additionally, I want anything that is myurl.com/adwords/(anything) to be rewritten to myurl.com/(anything). My regex and htaccess skillz are weak. Here is what I have in .htaccess:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/adwords/(.*)$
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{QUERY_STRING} !^(ACT=.*)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
So I think I'm saying anything that ends in /adwords/(something), capture the something, and then append it at the end of index.php via $1. I'm guessing this is simple. Thanks!
The rule you are looking for as such is probably simply
RewriteRule ^adwords/(.*)$ index.php/$1 [L]
with no rewrite condition needed, but I wonder... do you really want to rewrite http://myurl.com/adwords/foo to http://myurl.com/index.php/foo, rather than to http://myurl.com/index.php?a=foo ?
If you also want to rewrite something like http://myurl.com/baa/adwords/foo to http://myurl.com/baa/index.php/foo, you have to omit the ^:
RewriteRule adwords/(.*)$ index.php/$1 [L]
BTW, you can test most of your rewrite rules here: http://htaccess.madewithlove.be/
In one of my projects I'm trying to redirect using .htaccess. I am using a redirect like this:
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
But, now I'd like to add an exception to the rewrite rules. I want http://example.com/admin/ to be redirected in the same way, only to a different directory. All of the other traffic should still go to the public directory. So i added a rewrite condition like this:
RewriteCond %{REQUEST_URI} !/admin/
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
This didn't do the trick. I am still being redirected to the public folder. I also tried this:
RewriteRule ^admin$ admin/ [L]
RewriteRule admin(.*) admin/$1 [L]
RewriteCond %{REQUEST_URI} !/admin
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
This only made things worse. I now get an 500 internal server error.
I have been searching for hours now, and I'm getting a bit tired of finding examples that show how to add an exception for a certain folder, but don't show how to redirect this traffic to another folder. Can you tell me what I'm doing wrong, or which other approach I could try?
A RewriteCond only applies to the rule directly beneath it. In other words, in your example, it's only applied to the empty path rule. Move the RewriteCond down one notch, and it will work better.
RewriteRule ^$ public/ [L]
RewriteCond %{REQUEST_URI} !/admin/
RewriteRule (.*) public/$1 [L]
Additionally, I find your rule
RewriteRule admin(.*) admin/$1 [L]
to be odd. A URL like /adminsomething will be rewritten to /admin/something which I don't think is what you want.
Furthermore I have a hunch that you're confusing a rewrite with a redirect. A rewrite is a rule that makes a request be rewritten internally and interpreted differently. A redirect is a rule that will tell the visitor's browser to go to a different address. Which one are you going for?
The magic keyword for a redirect is R=301 which means redirect with http code 301 Moved Permanently. For example, you may want to try...
# This first rule is actually redundant in this case so skip it...
#RewriteRule ^$ public/ [R=301,L]
RewriteCond %{REQUEST_URI} !/admin
RewriteRule (.*) public/$1 [R=301,L]