Apache HTACCESS routing with Codeigniter under what appears to be a root level folder - apache

I have an interesting issue that I can't seem to get through. We have a URL that has been setup to route all requests to that URL (via nginx on another server) to my Codeigniter app (that exists on another server). For example, the URL is like this:
http://www.site.com/myappname/controller/function/parameter
Under normal conditions in Codeigniter, it routes the first element after the trailing forward slash to a controller class, then the second to a function within the class, and the third/etc as paramaters of that function.
What I need it to do is to disregard the "myappname" in the URL and have it send through the URL without that. The "myappname" is NOT a folder on the server.
I'm not 100% sure if I need to add new HTACCESS rules, or if I can do this programmatically through Codeigniter.
My current HTACCESS file:
RewriteEngine on
RewriteCond $1 !^(index\.php|sitemap\.xml|robots\.txt|public|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
This is a basic, and straight forward HTACCESS setup for all Codeigniter apps, however, using this, and using the structure above, if I go to, http://www.site.com/myappname, with "http://www.site.com/myappname" set as my base_url in the Codeigniter config, Codeigniter then translates it as:
http://www.site.com/myappname/myappname
So...can anyone help? I also tried to add the following code to the top of my config/routes.php file:
$_SERVER['REQUEST_URI'] = str_ireplace('/verified', '', $_SERVER['REQUEST_URI']);
That actually works partially, but now when I request CSS/JS files, from the root, it doesn't pull those in correctly because I use the built in site_url() function to build dynamic links based on the site's "base_url" property. I don't think I can do this 100% programmatically inside Codeigniter (through overwriting system level functions) because my CSS/JS/etc exist in a /public folder in the root of my app (that's obviously excluded from routing via my RewriteCond).
Anyone have a suggestion?

Try adding a RewriteBase to your htaccess:
RewriteEngine On
RewriteBase /myappname/
RewriteCond $1 !^(index\.php|sitemap\.xml|robots\.txt|public|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]

Related

htaccess Remove directory from end of URL in apache

Ok, so I know this is a question that has been asked many times, however, I have not been able to find an answer to my particular case, so please do not shoot me down.
I have a website: http://gmcomputers.co.za.
I am redirecting this URL, using .htaccess file, to a subfolder to load the content:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) /gmcomputers/ [L,DPI,R=301]
Which works perefectly, except when I go to http://gmcomputers.co.za I get http://gmcomputers.co.za/gmcomputers/.
So my question is, how do I modify the above code to remove the /gmcomputers/ from being appended?
Please note I copied the code above from a website as I am not at all experienced in redirect, etc and am still learning. Also, the reason I am using .htaccess to redirect is due to there being other websites in the root directory and I therefore cannot edit any config files for Apache.
Thanking you.
You contradict yourself in your question. On the one hand you write that you want to redirect and that this "works perfectly", but then you write that you do not want that result.
My guess is that you actually do not want to redirect at all, but that instead you want to internally rewrite your requests to point to that server side folder. While the URL visible in the browser's URL bar does not show that folder. Is that what you are trying to ask?
If so take a look at this example:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/gmcomputers
RewriteRule ^ /gmcomputers%{REQUEST_URI} [END]
You might want to add an actual redirection to direct clients actually using the folder name in their requests:
RewriteEngine on
RewriteRule ^/?gmcomputers/(.*)$ /$1 [R=301,END]
RewriteCond %{REQUEST_URI} !^/gmcomputers
RewriteRule ^ /gmcomputers%{REQUEST_URI} [END]
Best is to implement such rules in the central http server's host configuration. If you do not have access to that you can instead use a distributed configuration file (typically called ".htaccess") located in the DOCUMENT_ROOT folder configured for the http host, if you enabled the consideration of such files in your host configuration . Though that comes with a number of disadvantages. Above implementation works likewise for both approaches.

Create Apache mod rewrite rule to process all request to subdirectory

I want to redirect all incoming requests to a subdirectory, but can't figure out how. I looked here but that did not work for me.
So, if someone visits www.example.com/test, I want it to redirect to: www.example.com/test/subdirectory, but the URL still needs to be www.example.com/test
I also need the parameters to work, for example www.example.com/test/index.php?action=home must redirect to www.example.com/test/subdirectory/index.php?action=home.
I hope someone can help me!
So in fact what you ask for is not a redirect.
A redirect means the web server will send a new url containing the subdirectory, the browser will then request this new url, and show it in the url bar of the browser.
What you want is to tranparently map an directory structure to a real directory structure which differs. This is what Apache can do easily with the Alias instruction. Mod-rewrite could also do that, with RewriteRules not containing the [R] tag, this would imply an internal rewrite, not a redirect HTTP response. A [QSA] tag would also tell mod-rewrite to keep the current query string parameters on the rewritten url. But Mod-rewrite is not always simple to understand, and your problem is simple enough for a basic Alias, which is almost always included in Apache (more often than mod-rewrite).
this should be ok:
Alias /test /test/subdirectory
Try adding the following to the .htaccess file in the root directory of your site.
Query string parameters are passed through by default.
RewriteEngine on
RewriteBase /
#if not an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite to subdirectory
RewriteRule ^(.*)(/[^/]+)?$ /$1/subdirectory/$2 [L,QSA]

How can I write the below htaccess rewrite rule for different urls

I am working on a website created using PHP.
Development environment is Windows (using xampp)
The pages of website are displayed according to the value of the "page" variable that we pass to the index.php file as a url variable.
i.e. http://example.com/index.php?page=xyz
The url above points to the xyz.php file
There are around 50 pages (can be increased in future).
I want my urls to be like this
http://example.com/xyz
for this to work I wrote the following .htaccess rule in the .htaccess file (in the root folder of project)
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]
But it is not working at all.
Another url is
http://example.com/index.php?page=abc&name=gaurav&id=10&page_no=2
I want this url to be shown like this
http://example.com/abc/gaurav/10/2
There are many different forms of the url that I took example of but the page variable will always be there in the URL.
If I provide the rule for both the above urls then nothing works (500 internal server error is shown)
Please help me write this rule.
Try
RewriteRule ^([^/]+)$ index.php?page=$1 [L]
So a + instead of a star. It will use all characters till he finds a forward slash (/)

Rewriting a redirected URL with mod_rewrite

Here is my setup :
I have a website located at www.cabsh.org/drupal
I want to use mod_rewrite to do 2 things :
Redirect www.cabsh.org to http://www.cabsh.org/drupal/index.php (I got this one)
Rewrite /www.cabsh.org/drupal/index.php to www.cabsh.org/site/index.php
I cannot figure how to achieve the 2nd point. I'm using .htaccess files since I cannot use the main server configuration. Can anyone help me getting this to work?
Thanks!
From what I get from your comment, you just want something like this:
RewriteEngine on
# Prevent a request directly to the /drupal folder
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/drupal/
RewriteRule ^drupal/(.*)$ /site/$1 [R=301,L]
# Change a request for /site/(anything) to /drupal/(anything)
RewriteRule ^site/(.*)$ /drupal/$1
Be careful though, since Drupal (being in the Drupal folder) might generate links that point to /drupal instead of /site, which is seemingly not what you want.

Apache Rewrite: directory tree to subdomain directory

I have a web application that has one set of files used by 50+ clients and all the configuration for each site comes from a config.php file in their respective directories. This is accomplished with PHP parsing the URL. All this works fine, just having an issue with custom uploaded documents the client can do and are located in
/var/www/sites/user1/cache
There can be multiple subdirs. So when requesting
http://user1.site.com/cache/subdir1/image.jpg
it needs to be read from
/var/www/sites/user1/cache/subdir1/image.jpg
The client is allowed to upload any file type, so I just need the rewrite to take any /cache requests, then grab the subdomain and point to proper directory.
Came up with this, but am still getting an invalid page
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\.]+)\.site\.com$
RewriteRule ^cache/(.*)$ /sites/%1/cache/$1 [L]
Any help is appreciated.
If I read the RewriteRule documentation correctly, the L flag on its own would generate an internal redirection, meaning that the substitution would be interpreted as a local file system path.
Try using the complete path:
RewriteRule ^cache/(.*)$ /var/www/sites/%1/cache/$1 [L]
or do an external redirection (using HTTP return status "302 MOVED TEMPORARILY"), to let the user's browser re-send the request with the new path:
RewriteRule ^cache/(.*)$ /sites/%1/cache/$1 [L,R]
The /var/www/ is where the files are on the filesystem. I was routing based on the document root so I didn't need to put that there. But I realized I was missing the leading forward slash on the /cache/. Though your answer wasn't really what I was looking for, it made me see what I was missing. Thanks.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\.]+)\.site\.com$
RewriteRule ^/cache/(.*)$ /sites/%1/cache/$1 [L]