.htaccess files possibly conflicting? - php

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

Related

I'm getting a 404 page not found except home page

I have created my website using kohana framework and hosted in godaddy. I'm getting 404 page not found error in all links except home page. I have attached .htaccess file here. Kindly provide any ideas regarding this isuse.
# Turn on URL rewriting
RewriteBase /
RewriteEngine On
# Installation directory
#RewriteBase /kohana/
# Protect application and system files from being viewed
RewriteRule ^(application|system) - [F,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
# RewriteRule .* index.php/$0 [PT,L]
RewriteRule ^(.*)$ index.php?/$0 [L]
Make sure that .htaccess is in the same directory as all .php files are. /public_html/ or a folder like this.
Working Example
RewriteEngine On
//Optional: Redirect www to non www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
//Optional: Redirect http:// to https://
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
//Edit the URL (Without parameter)
RewriteRule ^login/?$ login.php [QSA]
RewriteRule ^logout/?$ logout.php [QSA]
//Edit the URL (With parameter)
RewriteRule ^user/edit/([0-9]*)/?$ user_edit.php?user_id=$1 [QSA]
If you are hosting on GoDaddy you usually don't need to make a .htaccess for the index.php file.
It happend to me once, i don´t know if it will be the same problem for you but i know that some hosting servers work using case sensitive urls
Example:
if your file has this name= "Login.php"
and if you try to acces to it like this= "login.php" it will not work and that is also applied to the name of folders.
It looks like you got a question mark in your last line that doesn't belong there, it should be:
RewriteRule ^(.*)$ index.php/$0 [L]
Or else try:
RewriteRule .* index.php/$0 [PT]

.htaccess help - Codeigniter

I currently have an .htaccess file to rewrite index.php out of all of the base URL's, example example.com/index.php/home -> example.com/home
The .htacces file is as follows in the root of my public_html folder:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|adminer|resources|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
As far as I know, this rewrites any /URL that isn't images, css, resources, etc.. to point toward index.php.
I am now trying to add to my .htaccess a rule to rewrite example.com/forums -> fourms.example.com.
With the current rules in place, I receive a 500 - Internal Server Error when I try to visit forums.example.com. I have also tried the following to no avail:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|adminer|resources|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_URI} ^/forums(.*)
RewriteRule ^forums(.*)$ http://forums.example.com$1 [R=301,L]
How do I change these rules to allow the redirection of index.php and rewrite example.com/fourms to fourms.example.com?
And for the second time today, I have found the answer moments after posting a question:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|resources|forums|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_URI} ^/forums(.*)
RewriteRule ^forums(.*)$ http://forums.example.com$1 [R=301,L]
I had to exclude /forums from the first rewrite, and add in the second rewrite.

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]

.htaccess is causing infinite loop when used from subfolder, works fine in domain root

I am using this .htaccess code for PHP application.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(login|logout|public)($|/.*$) [NC]
RewriteRule . index.php [L,QSA]
RewriteRule ^login login.php?$1 [L]
RewriteRule ^logout logout.php?$1 [L]
If i use it within domain root www.website.com then it works fine but if i use it in a folder www.website.com/app it causes infinite loop.,
Any ideas on what i am doing wrong here?
Also, Is there a way to remove websitename.com in given .htaccess and make it completely dynamic so it works as it's supposed to without me entering the domain name manually for every different website.
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?websitename.com/ [NC]
RewriteRule \.(jpe?g|gif|bmp|png|txt|pdf|zip|rar|psd|doc|docx|xml|csv)$ - [F,NC]
Problem is not using end of line anchor $ in following 2 rules:
RewriteRule ^login login.php?$1 [L]
RewriteRule ^logout logout.php?$1 [L]
This is causing infinite loops since source URI /login and target /login.php are both matching regex: ^login
Replace your rules with these:
RewriteRule ^login/?$ login.php?$1 [L]
RewriteRule ^logout/?$ logout.php?$1 [L]

.htaccess misreads directories

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

Categories