I am stuck at something that might seem nothing less but stupid - stuck with a .htaccess RewriteRule.
The Problem:
I am trying to redirect all traffic that comes to the "project" webistes' "non-existant" pages to the index.php as a parameter "q". For example, lets say a user has come to the following URL:
http://localhost/project/products/
Now obviously there is no directory or file called products, hence,
For the above HTTP request, I want to translate this as:
http://localhost/project/public/index.php?q=products
The Setup:
Server - Apache 5.3.x with XAMPP
Directory Structure : htdocs/project/public/index.php
What I have already done in an attempt to solve the problem:
Placed a .htaccess file in project directory >> http://localhost/project/ >> in an attempt to transfer all the traffic to the public directory. The rule for that looks like this...
<IfModule mod_rewrite.c>
RewriteRule ^$ public/ [L]
</IfModule>
Above thing is working and all users getting redirected to the index.php page.
Placed another .htaccess file in the public directory with the follwing code:
<IfModule mode_rewrite.c>
RewriteCond....
RewriteCond....
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
Now if a user types the URL at the bar: http://localhost/project/hi
I want it to be translated as http://localhost/project/public/index.php?q=hi
Instead of that it is giving a 404 Error. Also please consider that the actual request being translated to the Apache should not be reflected in the URL bar as a permanent redirect [301].
Does anybody have a solution ?
Your htaccess isn't on the server root.
Then you must define the base url, and use a full path for your rewrite rule :
RewriteEngine on
RewriteBase /project/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /project/public/index.php?q=$1 [L,QSA]
If that file index.php is located at /public/ than you should do
<IfModule mode_rewrite.c>
RewriteCond....
RewriteCond....
RewriteRule ^(.*)$ /project/public/index.php?q=$1 [L,QSA]
</IfModule>
Related
I am trying to build a custom mvc framework in php and its folder structure is something like this in my localhost.
Controllers(Dir)
Models(Dir)
Views(Dir)
Lib(Dir)
Config(Dir)
Lang(Dir)
Public
.htaccess
In this .htaccess file I am redirecting all the request in the public folder and the code in this .htaccess file is as follows
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
And the following is my public folder contents
css(Dir)
js(Dir)
images(Dir)
mod(Dir)
index.php
.htaccess
In this htaccess I am checking if the requested uri is not the folder or file in this folder then pass that request to index.php, which in turn does the url processing for controllers and actions etc. And its code is as follows
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %[REQUEST_FILENAME] !-f
RewriteCond %[REQUEST_FILENAME] !-d
RewriteRule ^(.*)$ index.php [PT,L]
</IfModule>
Everything is working fine except when I try to use any css file,js, image or any php file in mod directory(I am calling those file via ajax) it still pass that request to index.php which again try to locate them as a controller etc.
So please can anyone tell me what I am doing wrong and how can I solve this problem. I still don't fully understand Rewrite Rules and Conditions so its possible that I have done something wrong in my rules.
Thank you,
Anchal Bhargava
I want to load the draft.php file when i type the address in browser: localhost:81/mvcdemo .I have added this code in my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ draft.php?url=$1 [QSA,L]
I have also changed Apache(httpd.config) file to get the rewrite on. So, what am i doing wrong?
You have conditions defined so that your RewriteRule will only be triggered, if the requested file, directory or link does not exist.
So if your .htaccess file is inside the directory mvcdemo and you're requesting said directory, the rule will not be triggered, because the directory exists.
Requesting http://localhost:81/mvcdemo/foo should trigger the rewrite rule, as it doesn't exist.
If you want all requests to the directory to be rewritten, then you can just do away with the conditions:
RewriteEngine On
RewriteRule ^(.*)$ draft.php?url=$1 [QSA,L]
Note that RewriteBase / sets the base URL as the document root, and since draft.php seems to be in the mvcdemo directory itself, you should do away with, as it will result in a draft.php not found error.
You could change apache's DirectoryIndex directive in your config file (or via an .htaccess) and append draft.php. Then it's auto-loaded like the usual index.html or index.php files.
I have just uploaded all file of my test website to 000webhost.com domain.
My site is farazphptest.comxa.com
I was working on Codeigniter before uploading the site. All of my controllers are working fine on my localhost, but after uploading the site on server when i try to access a controller say farazphptest.comxa.com/welcome where "Welcome" is my webpage/controller, it does not work and opens "error404.000webhost.com/?" All of my controller including "welcome" are working fine on localhost, but after uploading website on domain only the home page loads perfectly.
This is the 1st time i have put a site on web, do tell me if i am missing out some tricks. I have just simply copied my file on web as stated. Thanks
Here is my .htaccess file used in both for localhost and on server.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /faraztest
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /faraztest/index.php
</IfModule>
An alternative solution is to create sub-domain on 000webhost.com with project name , and call the project from sub domain url Directly, it will be like this :
From Cpanel got to Subdomains
Create new subdomain with project folder name. ex : test.farazphptest.comxa.com
Overwrite the created subdomain folder with your project folder. ex : test
Edit your .htaccess in your main uploaded project folder to be like this :
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
now you can call your project directly from the new subdomain url.
ex : http://test.farazphptest.comxa.com
Following solved my problem
My .htccess file is
# index file can be index.php, home.php, default.php etc.
DirectoryIndex index.php
# Rewrite engine
RewriteEngine On
# condition with escaping special chars
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
and it worked like charm!
I am using kohana 3.2
My site is www.mysite.com/best, kohana is installed on best.
I am using the default htaccess file with rewritebase /best
so what is the best way to redirect users that go to www.mysite.com?
Right now if someone puts www.mysite.com/helo (www.mysite.com/best/helo)
It loads but apache gives 404 not found.
I hope my question makes sense.
EDIT
If a user goes to main domain (www.mysite.com) I want it to load kohana located at mysite/best.
but if a user right now types the full thing and omits best, site will load but my forms wont work because it is posting to www.mysite.com/helo not www.mysite.com/best/helo
instead of loading the fake helo controller, it must show a page not found.
EDIT
Ok I added this to htaccess file and it works perfectly
RewriteCond %{REQUEST_URI} !^/best/
RewriteRule ^(.*)$ /best/$1
You can't have multiple rewritebase in htaccess so...
rewrite your rules to fit you need in /best
or put an other htaccess in / to rewrite your needs.
Try this .htaccess instead:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /best/
# 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?kohana_uri=$0 [PT,L,QSA]
RewriteRule .* index.php?/$0 [PT,L,QSA]
Note that the trailing / on RewriteBase is required.
Can any one help me in .htaccess.
My use case is, if there is index.php in the directory http://domain_name/user/ then load index.php else set rewriteRule to call forbidden.php.
I did a bit but not succeed yet. .htaccess code on my server is-
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond ^(.*)$/index.php !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.*)$ forbidden.php?handle=$1
</IfModule>
I think there is an easier way to do what you want. There is no need for rewriting, simply define which page acts as the entry page. In the .htaccess file you can define:
DirectoryIndex index.php
ErrorDocument 404 /forbidden.html
Now, if anybody calls your directory http://www.example.com/user/, the page http://www.example.com/user/index.php will be shown.
If the file index.html does not exist, the server will return an error, so you can define an appropriate error page. With a leading /, you can define a single error page in the root directory, without the / it will look in the relative directory http://www.example.com/user/forbidden.php.