weird htaccess question - .htaccess

Currently this is my .htaccess
RewriteEngine on
#rewrite the url's
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
So, from the index i render a template, and give it a url.
Normally a page would look like this
www.whatever.com/?url=test/page
But with the rewrite it goes
www.whatever.com/test/page
So the question is {
I have an admin section of the site that I want unaffected by this.
So, /admin needs to access the admin folder in the folder tree.
Thanks for the help
-Wes

The best way to do this is to not re-write the URL's of real files and directories on the filesystem. This can be achieved by adding a couple rewrite conditions to your rule:
RewriteCond %{REQUEST_FILENAME} !-s [OR]
RewriteCond %{REQUEST_FILENAME} !-l [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
Now, these mean, respectively, only rewrite urls that are: not a real file (with > 0 size), not a symlink, and not a directory.
Alternatively, you could just make sure your rule does not match your admin directory:
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
The first example is by far the most flexible, however, as it won't interfere with any static files, such as images, etc.

Related

rewriting url with .htacces cause files not to be found

In general, I am trying to understand how .htaccess works. I would highly appreciate it if anyone points me in the right direction. I have been trying to make the following url (with optional parameters) pretty.
mysite/v1.0/foldername
mysite/v1.0/foldername/param1/
mysite/v1.0/foldername/param1/param2/etc
I tried the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ foldername.php [QSA,L]
the problem is that when I get it to pass the parameters it can no longer get the resources. It seems to have changed directory.
.htaccess is in foldername
Also, I would like to know what site i can go to to learn about REQUEST_URI, REQUEST_FILENAME, etc. A site that is not too technical as it's the apache site.
You are incorrectly rewriting the rules correct rule according to your need would be like,
RewriteEngine On
# rule for removing extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([\w-]+)/?$ $1.php [QSA,L]
# below cond means incoming url is nor a file neither a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# actual rules
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?param1=$1 [L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ $1.php?param1=$2&param2=$3 [L]
Refrences
Reference: mod_rewrite, URL rewriting and "pretty links" explained
http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Multiple rewrite conditions for multiple parameters

There is an issue with the htaccess rewrite conditions in my setup.
Currently I have the following code.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://mydom.com/$1.php
This works fine for any base page making them look like this.
http://mydom.com/page
What I want to also be able to do is add parameters from the url if they exist. I have some pages that will be like this.
http://mydom.com/page?param=1&secondParam=2
What I've tried to do is add this.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)$ http://mydom/$1/$2/$3 [L]
RewriteRule ^(.*)/(.*)$ http://mydom/$1/$2 [L]
This made sense to me, because I thought if the condition didn't match, it would move on, but this gave me an internal server error.
What I ended up doing was setting up a separate rule for each page that could have multiple parameters like this.
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/(.)/(.)$ http://mydom.com/page.php?param=$1&secondParam=$2 [L]
RewriteRule ^page/(.*)$ http://mydom.com/page.php?param=$1 [L]
RewriteRule ^(.*)$ http://mydom.com/$1.php
This works, however you need to keep in mind relative links you may have in your site such as style sheets and javascript files. In my case, I had to replace all relative paths with full site paths, depending on the way you set up your site, it could take a while to replace.

Remove index.php from url for ONLY CI subdirectory

I get how to remove index.php from CI urls overall. The following works fine:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /crm/v2/index.php?/$1 [L]
However, it interfers with other code on the live site. This new CI version i'm adding is in a subdir named v2. Thus the layout is something like:
-originalDir
--v2
-otherDirs
-.htaccess
-etc...
So in essence urls come out something like:
// Original page
https://www.site.com/originalDir/somepage.htm
// And this exist too
https://www.site.com/originalDir/index.php
// My new stuff
https://www.site.com/originalDir/v2/index.php/controller/etc...
// Desired effect withOUT affecting the original sites index.php page
// aka, the below is what i WANT but NOT getting!
https://www.site.com/originalDir/v2/controller/etc...
I can code in a lot of languages, but never a lot of experience with these htaccess rewrites. Any ideas on how to make it rewrite index.php for ONLY the codeigniter sub-directory? I've tried a few things that seem to work locally, but not on the live server. Not sure the exact structure of rewrite code.
Assuming you want to only rewrite rules in the CI directory, and not point any URLs outside of that directory to it...
Change your RewriteRule to match only URI's that start with your CI directory.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^crm/v2/(.*)$ /crm/v2/index.php?/$1 [L]
Alternatively, you can put an .htaccess file in your /crm/v2 directory, and specify a RewriteBase.
RewriteEngine on
RewriteBase /crm/v2/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Have you tried using RewriteBase?
RewriteEngine On
RewriteBase /crm/v2
RewriteRule ^(.+)$ index.php?/$1 [L]
I have not tested this specifically with your information but this is from a template that I use for some of my projects.
Here is the documentation for RewriteBase: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
I hope this helps!

Do I need to call for .htaccess?

I have no experience with .htaccess, but I got a tip that it's very useful so I wanted to try this.
I now have a file called .htaccess, in my root folder.
The files contains this;
RewriteBase /
RewriteEngine on
RewriteCond %{HTTP_HOST} ^kellyvuijst\.nl [nc]
RewriteRule (.*) http://www.kellyvuijst.nl/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
What I'm trying to do here is create a 'www.mysite.com/portfolio/' instead of 'mysite.com/portfolio.html' I used some tutorials on this and I think it's correct, but I'm not sure.
So now I have this file, and what now? The tutorials all show what to put in the file but not what to do with it? Do I need to call for it in every .html page I have? And how do I call for it?
A .htaccess file is automatically invoked by the server.
You have just to put this into your file :
RewriteBase /
RewriteEngine on
RewriteRule www.mysite.com/portfolio/ /mysite.com/portfolio.html [L]
Hmm, you're using a lot of rules here to achieve just that.
Anyway, no you don't have to include that file. If you're hosting your site on a server with Apache it'll be included automatically. Can you also run PHP files or is your site just HTML? That's always an easy sign if you're also using Apache (not 100%, but often the go together).
If so, you could try just using these rules first:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.+)\.(.+)$ [nc]
RewriteRule ^(.*)$ http://www.%1.%2/$1 [R=301,L]
If that always adds www to your address, even if you type in the URL without www at least you can be certain that it works.
Then, to make the .html disappear you can add this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule $(.*)/$ /$1.html [L]
This should make every url that ends with a slash (like portfolio/) use a .html file instead (portfolio.html), but only if /portfolio/ isn't an actual directory on your website.
(I removed your url from the rules because this way it should also work if you use it on another website, or if you change your url. It should still do what you want)
Made sure the server is configured to allow htaccess files to override host options. So in your vhost/server config, you need:
AllowOverride All

Rewrite rule to hide folder, doesn't work right without trailing slash

i have a strange apache mod_rewrite problem. I need to hide a sub-directory from the user, but redirect every request to that sub-directory. I found several quite similar issues on stackoverflow, but nothing really fits, so i decided to post a new question.
My .htaccess looks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)?$ foo/$1 [QSA,L]
The document-root only contains the following folder/files:
/foo/bar/index.html
I would now expect that example.com/bar and example.com/bar/ would just show me the contents of index.html.
Instead example.com/bar/ show me the content as expected but example.com/bar redirects me with a 301 to example.com/bar/foo/ an then shows the contents. I really don't get why there is a 301 redirect in this case.
When i put something this
RewriteCond %{REQUEST_URI} !^[^.]*/$
RewriteCond %{REQUEST_URI} !^[^.]*\.html$
RewriteCond %{REQUEST_URI} !^[^.]*\.php$
RewriteRule ^(.*)$ $1/ [QSA,L]
on top of that rule it seems to work, but that would require me to list every used file extension...
Is there any other way i can omit the redirect, the folder "bar" should never be seen by an outside user.
Thanks in advance!
1st rewrite rule is redirect from /foo/(.) to ($1) and second - from (.) to $1.
just idea, this has not been tested.
Better late than never...
Got it working with a simple RewriteRule which append a / to every url that doesn't have on.
# only directories
RewriteCond %{REQUEST_FILENAME} !-f
# exclude there directories
RewriteCond %{REQUEST_URI} !^/excluded-dirs
# exclude these extensions
RewriteCond %{REQUEST_URI} !\.excluded-extension$
# exclude request that already have a /
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [R=301,L]

Resources