.htaccess misreads directories - php

I have a setup that sets variables for the index page, but it also does the same for directories and I don't want it to do that. Here's my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js) [NC]
RewriteRule ^([a-zA-Z0-9]+)$ index.php?name=$1
RewriteRule ^([a-zA-Z]+)/$ index.php?name=$1
RewriteRule ^([a-zA-Z]+)/([a-zA-Z0-9_]+)$ index.php?name=$1&page=$2
RewriteRule ^([a-zA-Z]+)/([a-zA-Z0-9_]+)/$ index.php?name=$1&page=$2
RewriteRule ^php/$ error/
Now, with this setup, if I type in mysite.com/login it will redirect to the index.php page and set login as the name variable. How do I make it to where it ignores the directories? This is frustrating me and I've looked through this site for over an hour for an answer and can't find a similar question (I might suck at that too, though. haha!).
Also, if you look at the last RewriteRule, you can see that I'm trying to redirect any attempt to access my php/ folder to my error/ folder. This is also not working.

RewriteCond only applies to the immediately following RewriteRule. Also, you can combine lines 3&4, and 5&6 respectively, by using /? on the end, which makes the / optional (regex).
Your file could be similar to this:
RewriteEngine On
#the following line will not rewrite to anything because of "-", & stop rewiting with [L]
RewriteRule ^(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js)/?(.*)$ - [L,NC]
RewriteRule ^php/?(.*)$ error/ [L,NC]
RewriteRule ^([^/]+)/?$ index.php?name=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?name=$1&page=$2 [L]
You may be interested in the Apache Mod_Rewrite Documentation.

RewriteCond %{REQUEST_URI} !^/(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js) [NC] !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
to either not execute the rule on a directory or a file
More info on the mod_rewrite documentation pages, search for CondPattern

Related

.htaccess in subfolders

I read tons of topics about this here on SO, but for some reason it not works for me, and I am totally confused. I know, I mess up something, I just cant figure it what.
I have a site, and the index.php is calling the router, and show the content that I want.
I've created an admin' page, and I want the webserver to use the /admin/index.php for every request what is starting with /admin/
Here is what I try:
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin/(.*)$ admin/index.php?action=$2 [L,NC]
RewriteRule ^(.*)$ index.php?action=$1 [L]
I echoing some information from both index.php to see which handles my request.
If I am writing http://localhost it says Frontend.
When I try http://localhost/admin/ it says Frontend again.
If I am moving the admin' line below the first rule like this:
RewriteRule ^(.*)$ index.php?action=$1 [L]
RewriteRule ^admin/(.*)$ admin/index.php?action=$2 [L,NC]
then http://localhost/admin/ says Admin what is actually good!
But if I try http://localhost/admin/users what is actually not there, because the admin's router should handle that, it says Frontend.
How should I set .htaccess to use /admin/index.php in all cases, where a http://localhost/admin/ in the URI?
Have it like this:
RewriteEngine On
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^admin/(.*)$ admin/index.php?action=$1 [L,NC,QSA]
RewriteRule ^(.*)$ index.php?action=$1 [L,QSA]

rewrite url to subdirectory on subdomain

I have all of my customers sites in a directory on my subdomain:
customers.example.com/sites/customer_name
I want to rewrite the url so my customers only need to write
customers.example.com/customer_name
I have tried some different htacces scripts but none of them works. And one of them gives me a error 500 internal server error, so my mod_rewrite is active
Here is my current .htaccess file:
RewriteEngine On
RewriteRule ^sites/(css|js|img)/(.*)?$ /$1/$2 [L,QSA,R=301]
RewriteRule ^(.*)$ /sites/$1/ [QSA,L]
Since the rewrite engine loops, this pattern ^(.*)$ will blindly match everything, including your rule's target: /sites/something. It'll continue to append /sites/ to the front of the URI until you end up with something like /sites/sites/sites/sites/sites/sites/sites/ etc.
You need to add some conditions to prevent the looping, something like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /sites/$1/ [L]
or
RewriteCond %{REQUEST_URI} !^/sites/
RewriteRule ^(.*)$ /sites/$1/ [L]
or
RewriteCond %{DOCUMENT_ROOT}/sites%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/sites%{REQUEST_URI} -d
RewriteRule ^(.*)$ /sites/$1/ [L]
This should work… If understand your end goal correctly. But unclear if you want the content accessible from customers.example.com/sites/customer_name and customers.example.com/customer_name at the same time:
RewriteEngine on
RewriteRule ^/sites/(.*)$ http://customers.example.com/$1 [L,R=301]

mod_rewrite /foo to /index.php?id=foo AND /foo/foo2 to /index.php?id=foo/foo2

I have a problem with mod_rewrite.
I want to redirect those URIs in the title. I use the following rule
RewriteEngine on
RewriteCond $1 !^(folders not to be redirected e.g. css|images)
RewriteCond $1 !^(.*).(file extension e.g. png|css|txt|php)
RewriteRule ^(.*)$ index.php?id=$1 [L]
It work only if I put all the resources in a folder, else it would tell me:
"/foo/index.php" not found.
So to solve that I put all the resources in the folder "www"
But when I try to load the resources from a subfolder of e.g. "foo" it tells me:
The requested URL "/foo/foo2" was not found on this server.
How can I load resources from a subfolder like "/foo/foo2" or even "/foo/foo2/foo3"?
And how can I solve the problem with the automatic search for index.php in a folder?
I believe you can use the following to achieve your desired result. It doesn't filter by file extension, but rather checks to see if the file actually exists. One thing that is a little different is that it first appends a trailing slash to your links, something you may not want.
RewriteEngine on
RewriteBase /
# This appends a trailing slash. You will have to update http://so with your domain.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://so/$1/ [L,R=301]
# This does the internal redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ /index.php?id=$1 [L]
If you don't want the tailing slashes, you could use the following
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?id=$1 [L]

.htaccess files possibly conflicting?

I have one .htaccess file in the public_html folder of my server that lets me keep my primary domain in a subfolder:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mrmikeanderson.com$
RewriteCond %{REQUEST_URI} !^/mrmikeanderson/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mrmikeanderson/$1
RewriteCond %{HTTP_HOST} ^(www.)?mrmikeanderson.com$
RewriteRule ^(/)?$ mrmikeanderson/index.php [L]
In that subfolder is another .htaccess with more rewrites to turn urls ending with things like /index.php?page=about into just /about:
RewriteEngine On
RewriteRule ^$ index.php?page=home
RewriteRule portfolio index.php?page=portfolio
RewriteRule resume index.php?page=resume
RewriteRule about index.php?page=about
RewriteRule contact index.php?page=contact
The last four pages work, but my rewrite for just the domain name (\^$) is broken. Everything works on my local MAMP server, but the first .htaccess file is not present there, so I'm thinking that the two are conflicting. Any web dev champs able to see what's going wrong?
I'm assuming you have a /mrmikeanderson/ folder where the 2nd htaccess file is. The reason why the
RewriteRule ^$ index.php?page=home isn't being applied is because you are redirecting the / request to mrmikeanderson/index.php. So either change this rule:
RewriteRule ^(/)?$ mrmikeanderson/index.php [L]
to
RewriteRule ^(/)?$ mrmikeanderson/index.php?page=home [L]
or change this rule in the other htaccess file:
RewriteRule ^$ index.php?page=home
to
RewriteRule ^(index.php)$ index.php?page=home
Or you can change your index.php file to assume the variable page is home by default.
Try commenting out:
RewriteRule ^(.*)$ /mrmikeanderson/$1
It looks like the regex ^(.*)$ will match anything including blank strings, which would conflict with RewriteRule ^$ index.php?page=home
Edit:
Try using ([A-Za-z0-9]+) in place of the ^(.*)$ which should give you:
RewriteRule ^([A-Za-z0-9]+)$ /mrmikeanderson/$1
You can always set up a rewrite log to see what's going on http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritelog

htaccess to php with different name/variable combos

I have php that works awesome
But I wanna change it from showing variables to look like dirs
eg:
example.com/?mode=page&page=15 to example.com/page/15
and
example.com/?mode=pic&picid=136 to example.com/pic/136
however I only know of one way to do it, such as:
RewriteRule ^(.*)$ index.php?mode=$1&page [R]
but that only works for one case, otherwise it 404s
I tried using RewriteRule ^(.*)$ index.php?url=$1 [L,QSA] so that the whole path gets passed. However, this way it doesn't seem to pass the CSS in my pages.
I'm extremely confused...
Here's my htaccess file
# Do not remove this line, otherwise mod_rewrite rules will stop working
RewriteBase /
IndexIgnore *
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^css/styles\.css$
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
The problem is you are rewriting ALL urls to
index.php?url=
So your CSS files also get "redirected" to "index.php?url=" eg
index.php?url=css/styles.css
So you need to add "conditions" to your .htaccess so that certain files (or directories) dont get "redirected". You can do this by using "RewriteCond" (conditions) before your "RewriteRule ".
For example to not "redirect" the css file "css/styles.css" you could do
RewriteCond %{REQUEST_URI} !^css/styles\.css$
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

Categories