.htaccess complex and tricky redirection - php

I am not expert in .htaccess that my need bit complex and tricky help..!
Here is my current .htaccess and remember i need some .htaccess that can destroy my current .htaccess.
Current .htaccess:
RewriteRule ^join$ https://example.com/internalpath/join/index.php [L,QSA]
RewriteRule ^join/(.*)$ https://example.com/internalpath/join/$1 [L,QSA]
Now my problem:
I want to redirect each and every file and directories to HTTP, except these two:
https://example.com/join/
https://example.com/join
Remember, I want every link on http except this above provided link, other then all files and directories and even files and directories in /join/ or /join will be HTTP, just two links on HTTP, but please it will not destroy the previous written .htaccess.

Try this (not tested):
RewriteCond %{REQUEST_URI} !/join/?$ [NC]
RewriteRule ^(.*)$ http://domain.com/internalpath/$1 [R=301,QSA,L]

You need these 3 rules:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(join/.+)$ https://%{HTTP_HOST}/internalpath/$1 [R=301,NC,L]
RewriteCond %{HTTPS} off
RewriteRule ^(join)/?$ https://%{HTTP_HOST}/internalpath/$1/index.php [R=301,NC,L]
RewriteCond %{HTTPS} on
RewriteRule !^join http://%{HTTP_HOST}%{REQUEST_URI} [R=301,NC,L]

Related

Redirecting a list of folders with .htaccess

I recovered a project but it is not generic at all. I looked a bit what was on .htaccess but it is not obvious. So I ask you, you will probably have the solution!
Currently my .htaccess file is
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.exemple.com$ [NC]
RewriteRule ^(.*)$ http://exemple.com/$1 [R=301,L]
#RewriteCond %{HTTPS} off
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule folderA/(.+) www/$1
RewriteRule folderB/(.+) www/$1
with copy of index.php files in folderA/index.php and folderB/index.php.
index.php are :
<?php
header("Location: index.php");
?>
I want the http://exemple/folderA/ , http://exemple/folderB/, http://exemple/folderC/ ... to be written and displayed in the browser but redirects to www/index.php
Commonly new folders are added by the old team. I would like to have the same behavior without the folders that multiply.
Can you help me ?
Thx
What I used was something like this:
RewriteCond %{REQUEST_URI} !www/
RewriteCond %{REQUEST_URI} folderA
RewriteRule (.*) /www/index.php [L]
RewriteCond %{REQUEST_URI} !www/
RewriteCond %{REQUEST_URI} folderB
RewriteRule (.*) /www/index.php [L]
This is a quick-n-dirty approach, that I used on a project recently because out glorious admins think that all code would be in the document root :(
Also, I have no idea whether it will work for you, and you probably could collapse those rules into one.

How to redirect if the url have index.php/somefileOrsomedirectory

What do i want to do is if the url have www.example.com/index.php/anything then it should throw the user to www.example.com/error-404
Here is my current expressions in my htaccess file.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule (.*)$ http://www.example.com/$1 [L,R=301]
Redirect 301 /online-features/key-anywhere-web-based-system http://www.example.com/online-features
Redirect 301 /home http://www.example.com/
Thanks in advance.
Please suggest/edit my question if i have not asked the question in correct way.
EDIT
I don't want to loose the /index.php to / redirection.
www.example.com/index.php/anything
/anything here is additional path information (after the name of a physical file). You can use the AcceptPathInfo directive to specifically disable this "feature":
AcceptPathInfo Off
In which case all URLs containing additional path info will automatically trigger a 404. However, the URLs can still be routed using mod_rewrite, which is probably what's happening here.
You will still need to implement a mod_rewrite redirect as mentioned in the datascript's answer. Or try something like the following, before your existing directives:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php/ /error-404 [R=302,L]
The check against REDIRECT_STATUS is to avoid a rewrite loop, since your front controller appears to use path info to route the request.
Change this to a 301 if this is intended to be permanent, once you have confirmed it's working OK.
However, it would be preferable if this was implemented as a proper Apache error document. For example:
ErrorDocument /error-404.php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php/ - [R=404,L]
I think the following should do what you require. The dot is any character and the + means one or more times.
This would require the forward slash to be there
RewriteRule ^index\.php/(.+)$ error-404 [L]
EDIT: Thanks #DocRoot, updated accordingly

htaccess https in parent but http in subdirectory

I've tried implementing a number of suggested solutions to this problem but can't get anything to work.
I am using 2 .htaccess files, one in my root directory which reads as follows:
RewriteEngine on
RewriteBase /
RewriteRule ^(webservice|api) - [L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.domain.com%{REQUEST_URI} [R=301,L]
This redirects all traffic to the main website through https which I need.
However in a subdirectory I want to disable the reroute and force all requests that go to this directory through http. The .htaccess file in /webservice/rest/ reads:
RewriteEngine On
RewriteBase /webservice/rest/
RewriteCond %{HTTPS} on
RewriteRule (.*) http://www.domain.com/webservice/rest/%{REQUEST_URI} [R=301,L]
RewriteRule ^testingPost/?$ testingPost.php [NC]
RewriteRule ^([a-zA-Z0-9\_\-]+)/?(\?([a-zA-Z0-9\_\-\=\?\&]))?$ otherfiledirect.php?method=$1&params=$2 [NC]
There are a few redirects taking place, but the 3rd and 4th line in the 2nd htaccess don't seem to be doing anything - can anyone see what I'm doing wrong and how to keep https on the main site but force http on any url below /webservice/rest/ ??
Cheers
The problem was with the ssl config file in the server. Allow Override needed to be set to All under the secure virtual host at port 443. After changing that, the following code in the root htaccess file allowed me to force https across the whole website except a specific folder:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !/webservice [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
So any request to http would go to https. However I still needed to explicitly change requests to https on my specified folder to redirect to http, so added the following code after the above:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} /webservice [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Bullet proof.
Try this in /webservice/rest/.htaccess and make sure to test in a new browser:
RewriteEngine On
RewriteBase /webservice/rest/
RewriteCond %{HTTPS} on
RewriteRule ^ http://www.domain.com/%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^testingPost/?$ testingPost.php [L,NC]
RewriteRule ^([\w-]+)/?(\?([\w-=?&]))?$ otherfiledirect.php?method=$1&params=$2 [L,NC]

.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 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