RewriteRule for a file in a parent directory - apache

I've got a site set up on an apache server with the desktop site in /public_html and a mobile site /in public_html/mob
I'm trying to set up an .htaccess rewriterule to send users to an index.php file in /public_html if they visit the /mob folder. My current rewrite rule, in the mob subfolder is:
RewriteRule ^(/)?$ ../index.php
I can load up the same file in the mob subdirectory with:
RewriteRule ^(/)?$ index.php
However I can't seem to get the site to load the index.php file from the parent directory (public_html).
When attempting to load http://www.domain.com/mob in a browser I receive:
Bad Request
Your browser sent a request that this server could not understand.
This same rewriterule worked fine on our development server, but doesn't work in our live environment.
The .htaccess file from the /public_html/mob folder is as follows:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(/)?$ ../index.php [L,QSA]
When index.php is reached a mobile device detect script decides whether to load the content from the desktop or mobile site.

Check DOCUMENT_ROOT of your m.domain.com.
If DOCUMENT_ROOT for your m.domain.com is /public_html/mob then you cannot load /public_html/index.php without doing a full redirect (or proxy) to http://domain.com/index.php
Just to clarify any web site cannot access files above its DOCUMENT_ROOT folder level.

If your rule's target starts with a /, that makes it an absolute URI starting from the document root, which I assume would be the public_html folder:
RewriteRule ^(/)?$ /index.php

Related

Unable to access sub-domain using .htaccess in my Laravel5.8 site on cpanel shared hosting

I got a Laravel 5.8 site hosted on shared hosting. I want to access my subdomain using .htaccess file but I'm unable to figure out how to make it work.
My directory structure looks like this:
public_html/
cdn/ (subdomain directory)
index.php
public/ (assets and index.php file reside here)
css/
js/
images/
fonts/
index.php (loads laravel site)
.htaccess (responsible to load index.php from the public directory)
So, basically, right now, if I try to access cdn directory like this http://example.com/cdn, that takes me to http://example.com/public/cdn and if I try http://cdn.example.com, the browser gives me the error message that This site can’t be reached.
I tried this but it does not work.
RewriteEngine On
# Handling subdomain
RewriteCond %{HTTP_HOST} ^(www.)?cdn.example.com$
ReWriteRule ^(.*)$ cdn/$1 [L]
# Loads index.php located inside laravel's public directory
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
ReWriteRule ^(.*)$ public/$1 [L]
When I try to access, http://cdn.example.com, the browser gives me the error message that This site can’t be reached.
It supposed to load index.php file located inside cdn directory.
Please help me out.
Thanks in advance!

unable to load php from interface folder

I am new to Apache and recently my developer passed me the website with htaccess to host on Apache 2.4 running on CentOS 7, the website is running on the Windows laptop but not on the server with the same htaccess.
The webpage works like this, when user hits the url at http://www.example.com, and click on the links, it will load the rest of the files from a folder called interface.
The original htaccess as follow:
RewriteEngine On
#this is for specific users
RewriteBase /v1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /v1/index.php?m=$1 [QSA,NC,L]
ErrorDocument 404 /v1/error.php?=error
However it only load the main page, and the rest will load as follow:
Not Found
The requested URL /example was not found on this server.
I have tried the recommendations on this page Generic htaccess redirect www to non-www
and this page .htaccess for cakephp
and this page Remove .php extension with .htaccess
however all of them not working as what we want.
Any way I can get it to work?

mod_rewrite to remove index.php from Codeigniter in subdirectory

Codeigniter applications commonly use mod_rewrite to exclude the string index.php from the url. I have two Codeigniter applications within the same domain. One Codigniter application is in the web root folder, another Codigniter application is in a subfolder of the web root folder.
Codeigniter application 1:
http://domain.com/index.php
Codeigniter application 2 (the landing page application):
http://domain.com/land/index.php
The two Codeigniter applications are each atomic and do not share any files between them. Every file in the Codeigniter framework is in public_html/ and again in public_html/land/. So I need to exclude the string index.php in urls addressing the root / folder and also exclude the string index.php in the /land/ subfolder.
The .htaccess file in the root folder uses the widely recommended mod_rewrite rules (code below) from the Codeigniter wiki, and this set of rules works well for the root Codeigniter application (application 1). These rules reside in web root folder.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 /index.php
</IfModule>
The above set of rules has no problem removing index.php from the urls in the root Codeigniter application. But this set of rules does not seem to allow the mod_rewrite rules in public_html/land/.htaccess to execute.
When I remove the mod_rewrite rules in public_html/.htaccess, then the mod_rewrite rules in public_html/land/.htaccess start being evaluated.
Is there a way to change the mod_rewrite rules in public_html/.htaccess to handle the special case of a url intended to access the /land/ subfolder?
I think the best solution might be to change the mod_rewrite rules in public_html/.htaccess to allow the mod_rewrite rules in public_html/land/.htaccess to execute when the subfolder is addressed in the url. I am open to any suggestions.
Pre-emptive answer to the question "why don't you just use a subdomain?" 1. Saving money on the SSL certificate. 2) Non-techical users are sometimes confused by subdomains for marketing the base domain name.
Pre-emptive answer to "why don't you combine the Codeigniter applications to use the same files in the framework?" Duplicating the framework files is an easy way to keep the versioning repositories separated.
The problem is the rules in public_html/.htaccess are rewriting the URL's going to /land/, you need a passthrough which makes it so nothing happens when /land/ is requested.Add:
RewriteRule ^land/ - [L]
before the rest of your rules.
Add a rule at the top to just go to the land subfolder if it's part of the request string. That way, the rules in /land/.htaccess will be executed instead of the subsequent rules in /.htaccess. So put this at the top:
RewriteRule ^land.*$ - [NC,L]
This will check if the request begins with 'land' and redirect it to the subdirectory, where .htaccess rules corresponding to that subdirectory will be applied instead.
The reason the existing rule checking for files and folders and not doing the rewrite if the request corresponds to one of them is because whatever follows 'land' in the request is probably not a real file, and so the rewrite rule fires.

Redirecting .html internally on Apache

I'm wanting to redirect all requests for .html pages to a perl script for processing. However, for some reason I'm also being redirected for pages with no .html like the root site / as well as directories like /images/
RewriteCond %{REQUEST_URI} \.(html)$ [NC]
RewriteRule .* /page.cgi [L]
How can I make it so it will only redirect requests ending in .html
What am I missing?
Requests with no specified file default to requesting index.html in a particular directory, per a different Apache config option. Maybe try putting a blank index.php file in those directories (assuming your server also runs php?). You'd have to add index.php in httpd.conf as a recognized index file. That way, requests for a directory with no file specified would default to index.php, which does not trigger your re-write rule because it's not a .html.

mod-rewrite to ignore the subdomain

I'm using a mod-rewrite for pretty URLs, meant to run on the domain root. Working fine but now I'm trying to make it run on a subdomain and it keeps giving "500 Internal Server Error".
The subdomain automatically redirects to the folder with that name on my hosting account (sub.domain.com shows the content of domain.com/sub/). Does it fail because this request is already being mod-rewritten automatically or can I simply change something in the htaccess to address the subdomain instead?
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
Contact your ISP and check if they are setting the DocumentRoot for the sub domains to the /domain/sub/ directory (it's probable) or alternatively using an internal RewriteRule to direct traffic to that directory (you can see if there's an external rewrite / redirect in place via Chrome or Firefox + firebug, use the developer tools to check the response header, on the network Tab). If they have set the document root you will need to copy or symbolically link ALL the files you want accessible via the sub-domain, to the /domain.com/sub/ directory e.g. the .htaccess, index.php, images, js and css files and sub directories. If they are using an internal rewrite, a quick tweak to your own existing internal rewrite, in the existing .htaccess file, should suffice e.g.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]