sharedHostingFolder
folder1
folder2
folder3
xampp htdocs folder
Folder Structure:
root/sharedHostingFolder
-need an .htaccess file here
-folder1
--index.php
--dude.php
-folder2
--hello.php
--index.php
---folder5
----index.php
-folder3
--hi.php
--index.php
Request Structure
Request "root/sharedHostingFolder/hello.php" will show file inside "root/sharedHostingFolder/folder2/hello.php"
Request "root/sharedHostingFolder/" will show file inside "root/sharedHostingFolder/folder2/index.php"
Request "root/sharedHostingFolder/folder1/dude.php" will show file inside "root/sharedHostingFolder/folder1/dude.php"
Request "root/sharedHostingFolder/folder1/" will show file inside "root/sharedHostingFolder/folder1/index.php"
Request "root/sharedHostingFolder/folder2/hello.php" will show file inside "root/sharedHostingFolder/folder2/hello.php"
Request "root/sharedHostingFolder/folder2/" will show file inside "root/sharedHostingFolder/folder2/index.php"
Request "root/sharedHostingFolder/folder3/hi.php" will show file inside "root/sharedHostingFolder/folder3/hi.php"
Request "root/sharedHostingFolder/folder5/" will show file inside "root/sharedHostingFolder/folder2/folder5/index.php"
I HAVE NO ACCESS TO php.ini file
.htaccess file
RewriteEngine on
DirectoryIndex index.php
RewriteCond %{REQUEST_URI} ^/folder1/ [OR]
RewriteCond %{REQUEST_URI} ^/folder2/ [OR]
RewriteCond %{REQUEST_URI} ^/folder3/
RewriteRule - [END]
RewriteRule ^ /folder2%{REQUEST_URI} [END]
httpd.conf file:
<Directory />
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Error:
object not found
I researched and found out .htaccess will do that easily . But, Really I don't know how to do that.
This is a strange question. TO me it reads a bit like a classical xy problem... As if you did not ask how to solve your actual task, but how to get something to work that you think might solve your task...
Anyway, looking at your examples I have the impression that little is to be done, mostly some exceptions that need to be implemented as rewriting rules. So something like that:
RewriteEngine on
DirectoryIndex index.php
RewriteCond %{REQUEST_URI} ^/sharedHostingFolder/folder1/ [OR]
RewriteCond %{REQUEST_URI} ^/sharedHostingFolder/folder2/ [OR]
RewriteCond %{REQUEST_URI} ^/sharedHostingFolder/folder3/
RewriteRule - [END]
RewriteRule ^ /sharedHostingFolder/folder2%{REQUEST_URI} [END]
This leaves requests to URIs starting with one of the folder names untouched, replying on classical file system mapping and defining the index.php script as fallback logic for those folders. And it maps everything else into folder2.
It does not get clear from your question what that "www" string is meant to represent. If that is meant to represent another folder level then you might have to add that level to the rules above.
For this to work the rewriting module needs to be enabled, obviously. Best is to implement such rules in the http server's host configuration. If you do not have access to that you can also use a distributed configuration file (".htaccess"), but you need to enable the interpretation of such file for the requested location (see the apache documentation about the AllowOverride directive).
Related
I have a website http://sharenotes.xyz/.
In this website users can save and share quick notes to others users.
There is a unique note id for each note. (id can only contain [0-9A-Za-z_] charachters).
Unique note id is present in the url http://sharenotes.xyz/hithere.
In this case hithere is the unique note id.
In actual the url is like
http://sharenotes.xyz/index.php?id=hithere.
My folder structure looks like -
and index.php file is present in public folder.
What will be the content of the .htaccess file to short the url from http://sharenotes.xyz/index.php?id=hithere to http://sharenotes.xyz/hithere and in which folder should I place that .htaccess file ?
I know php but I am new in htaccess file (stored in public_html folder).
UPDATE
I was forget to tell you something that -
There is folder named as public which servers all user accessible files.
So I have also hide the name public from the url throught .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
Options -Indexes
That's why you wouldn't see public in url.
This would be the required rewriting rule:
RewriteEngine on
RewriteRule ^/?([0-9a-zA-Z_]+)/?$ /public/index.php?id=$1 [END]
Best is to implement such rules in the actual http server's host configuration. Only if you do not have access to that, then you should use a distributed configuration file, often called ".htaccess". But that comes with a number of disadvantages. If you decide to use one, then place it inside the top folder of your hosts DOCUMENT_ROOT, so here inside the "public_html" folder.
Obviously the rewriting modules needs to be loaded into the apache http server for this. And if you are using a distributed configuration file then you also need to enable the interpretation of such files for that location (read about the AllowOverride directive in the documentation of the http server).
Most likely you will need to add further rewriting rules to sort out requests to other resources.
UPDATE
Considering your comments and the additional information you now added to describe your actual situation this variant probably is close to what you are looking for:
RewriteEngine on
RewriteCond /public%{REQUEST_URI} -f [OR]
RewriteCond /public%{REQUEST_URI} -d
RewriteRule ^ /public%{REQUEST_URI} [END]
RewriteRule ^/?([0-9a-zA-Z_]+)/?$ /public/index.php?id=$1 [END]
I feel like this is a rather common request, but I am too confused about .htaccess and couldn't find a solution by Google.
I have a Laravel instance in a subdirectory of the Apache2 htdocs. Now I would like to invisibly redirect all requests from the root domain to this folder (it should be the "root" website). But the tricky thing is, this is not the only folder, there are other folders directly in the htdocs, which should be reached normally. Just the "root" website is not in the root but also in a subfolder. For example:
https://domainA.com should load https://domainA.com/laravel/public (including possible query string or parameters, but invisibly for the user)
https://domainA.com/websiteB should be served as it is
https://domainA.com/websiteC should be served as it is
...
I assume, part of this solution will be to list all the websiteB, websiteC directories in the .htaccess, would it be possible to automate this?
Thanks in advance!
You can put a .htaccess in the folder you want to custom controle but you have to create some filter condition
<IfModule mod_rewrite.c>
RewriteEngine On
## RewriteBase /foo
## conditions to tell what to redirect ie on URI
## RewriteCond %{REQUEST_URI} ^/a-folder/
## not websiteB or websiteC
RewriteCond %{REQUEST_URI} !^/websiteB/
RewriteCond %{REQUEST_URI} !^/websiteC/
## if the file does not exist call index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ my/path/to/a/script.php [L]
</IfModule>
After you have to do something special in script.php for those HTTP calls
You can also rewrite the URI and pass it again to apache but things can be complicated after...
Note: This question has been asked before several times, but the answers are really bad, totally wrong and/or do not fit the above scenario (because there are several files called index.php). If you like, see [1].
I want to block direct access to all .php files in the application folder (see file structure image) via the .htaccess file in the root folder. There are some solutions for this on the web, but they miss one thing: They don't work if there is more than one file named index.php (which is a realistic scenario like the screenshot shows, see the file in the view/xxx/ folder):
Question: How to block access to all .php files, except the index.php in the root folder ?
In .htaccess:
RewriteEngine on
RewriteRule ^/application - [F]
The [F] option instructs it to issue a 403 Forbidden response on all matching URLs.
Or add a separate .htaccess file in /application containing just:
deny from all
Or in your Apache vhost definition:
<Location /application>
deny from all
</Location>
In addition to Niels Keurentjes excellent answer I would like to extend his solution according to my .htacces that uses some very common rewriting patterns (as a lot of people might run into the same problem):
When using URL rewrite rules, then the line RewriteRule ^/application - [F] has to be at exactly that place. It will not work if the line is placed before or below:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
# The new line, blocking direct access to every file in /application and deeper
RewriteRule ^/application - [F]
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
I have looked at several examples of htaccess configs for websites within sub-directories, and tried most of them without 100% success.
My setup is:
using Yii framework
htaccess at public_html/.htaccess
site located inside public_html/mysite directory
index handling all requests located at public_html/mysite/frontend/www/index.php
The status of the URLs:
www.mysite.com works fine [ok]
www.mysite.com/controller/action shows me the homepage [wrong]
www.mysite.com/mysite/frontend/www/controller/action works fine [wrong, the item above should work instead]
My .htaccess at the moment looks like this:
AddHandler application/x-httpd-php53s .php .html
Options +SymLinksIfOwnerMatch
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !^/mysite/frontend/www
RewriteRule ^(.*)?$ /mysite/frontend/www/index.php [L]
I have tried everything, but I have no idea why www.mysite.com/controller/action won't work :(
Any help would be really appreciated! Thanks!
I found the answer to this similar question to be helpful. Here is how my rewrite rules ended up:
#Forward all non-existent files/directories to Yii
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) subdir/index.php/$1 [QSA,L]
This takes all non-existent files/folders and sends them to the yii script with initial url appended. QSA appends any query string that may be present in the initial url.
You didn't mention if you configured Yii's Url Manager for clean URLs. You need to, otherwise Yii expects the "route" to appear as a GET param named "r". If you didn't, consult this section of the definitive guide
You dont need to edit .htaccess. You just need to move the Yii entry script (index.php) and the default .htaccess up from the subdirectory to the webroot (so that they reside directly under public_html). Once you move index.php and .htaccess to the root directory, all web requests will be routed directly to index.php (rather than to the subdirectory), thus eliminating the /subdirectory part of the url.
After you move the files, you will need to edit index.php to update the references to the yii.php file (under the Yii framework directory) as well as the Yii config file (main.php). Lastly, you will need to move the assets directory to directly the webroot, since by default, Yii expects the assets directory to be located in the same location as the entry script).
That should be all you need to do, but if you need more details, I describe the approach fully here:
http://muhammadatt.tumblr.com/post/83149364519/modifying-a-yii-application-to-run-from-a-subdirectory
I also didn't update the .htaccess file, easier to modify the httpd.conf virtual host for the subdomain and change the DocumentRoot to point to your yii folder.
I am working with a custom MVC PHP framework and the index page (acting as a router) receives a GET variable "do" which contains the path that it will route to. If this variable is not set, it defaults to the Auth controller, method login.
require_once('config.php');
$controllerAction = isset($_GET['do'])?$_GET['do']:"auth/login";
require_once('core/main.php');
Then the index page (source code above) passes this $controllerAction to the main.php file, which autoloads the main controller and then loads the requested controller.
Thus, the URIs in this framework are of the form mysite.com/?do=controller/method/variable and I need it to be in the form mysite.com/controller/method/variable.
Here is the .htaccess file I tried to use, it just didn't work (I have other htaccess files working on the same server so it's not an Apache problem) :(
RewriteEngine On
RewriteRule ^([^/]*)$ /?do=$1 [L]
Someone suggested that I can do this using PHP but I am not sure how to go about that.
Edit:
The error is that I get "This page cannot be displayed", 404 errors, whenever I try to directly access the mysite.com/controller/method links rather than the default mysite.com?do=controller/method
Further Edit
(please note that other virtual hosts work fine on my localhost):
(XAMPP) Apache Virtual Hosting Info:
<VirtualHost *:80>
DocumentRoot "D:\sites\mysite.com\root\wwwroot"
ServerName mysite.com
ServerAlias mysite.com
<Directory "D:\sites\mysite.com\root\wwwroot">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
File structure (Windows):
D:\
--sites
----mysite.com
--------#client_details
--------root
-----------#devfiles
-----------#vars_pwd
-----------wwwroot
--------------config
--------------core
--------------application
------------------controllers
------------------libraries
------------------models
------------------views
----------------------css
----------------------javascript
----------------------images
----------------------icons
First of all, there are some issues with your .htaccess contents. It's always a good idea to not rewrite if a file with the requested name exists. This allows you to have an img/ folder for your images or any other static content like css files, javascript, downloads, etc.. The first RewriteCond tells Apache to only rewrite if no folder with this name exists. The second one does the same with files. Then you probably want the QSA (i.e. Query String Append) option, which will pass all other GET variables to your script.
Under this conditions you can simplify the regex and use this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?do=$1 [L,QSA]
You might be surprised because this is more or less the same as others posted. I use similar things for many of my projects and I've just tested it, I can guarantee that it works. There must be something wrong with your apache config.
When you have problems with mod_rewrite, the first thing you should try is to enable the module itself. Type these commands as root in your shell:
a2enmod rewrite
/etc/init.d/apache2 restart
The first one activates the module (or complains with Module rewrite already enabled if everything is ok) and the second one restarts your Apache server. The path may of course be different on your server.
Then you have to make sure that your VHost config allows you to use .htaccess files and do rewrites. This means AllowOverride must be set to at least FileInfo (or All). You could also try to put the rewrite rules right into the config file. Your config should look similar to this:
<VirtualHost *:*>
ServerName test.example.com
ServerAlias www.test.example.com
DocumentRoot /home/sites/test/
<Directory "/home/sites/test/">
Allow from all
AllowOverride All
Options +Indexes
</Directory>
</VirtualHost>
Note that you have to restart Apache if you change anything in there.
If that all doesn't help, it's always a good idea to have a look at the error logs. On my system they're located at /var/log/apache2/error.log (debian). They might give you more information on what's going wrong.
Try
RewriteEngine On
RewriteRule ^([^/]*)$ index.php?do=$1 [L]
Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?do=$1 [L]
Check your apache logs, access logs specifically. If the folder is present in the web root, then you should be able to access it directly :). You might also want to check if you have duplicate virtualhost entries for the same site by chance.
This one is my customized MVC framework which is based on cake
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?do=$1 [QSA,L]
</IfModule>
May be this should help. The typical URL pattern for this site.com/controller/method
I don't know what your domain setup is like, but here are some suggestions.
If your code resides in the root of your folder, and the index file is called index.php try the following:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?do=$1 [L,QSA]
If your website exists in a subfolder e.g. www.example.com/site/, and the index file is index.php Then try the following (change /site/ to whatever your folder is).
RewriteEngine On
RewriteBase /site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site/index.php?do=$1 [L,QSA]
If you still get the 404 error message then do the following:
Make sure your site allows .htaccess files to be processed by checking AllowOverride is set to all. If you don't have access to the necessary config files to check, a simple test is to setup an .htaccess rule to redirect to a dummy file on your system. If it works, then your .htaccess is being executed fine.
Have a look at your MVC framework to see what page it's actually sending the request to. The problem may be that you haven't defined a handler for that particular request, and the default action of your MVC framework is to throw a 404 error.
Edit: Just reading your description, I notice you said that the URL should basically be something like mysite.com/?do=controller/method/variable. If it has be very strict about this format, then you'll also need to put in rules for removing any leading or trailing slashes, e.g. the following re-write rule should do it:
RewriteRule ^\?(.*)\?$ /index.php?do=$1 [L,QSA]
(This makes the leading and trailing slashes optional, but it should remove them from the actual value you pass to do).