On my Ubuntu development machine(s) I run a LAMP stack. For each website I work on, I create a new directory off root:
/var/www/somesite.com
/var/www/anothersite.com
The problem I have is that apache wont allow duplicate rewrite rules across these folders. For instance, If I set up this:
RewriteRule ^track/(.*)$ /somesite.com/order_track.php [nc,L]
http://localhost/somesite.com/track/abc123 - works as intented
This same declaration wont work on anothersite.com
RewriteRule ^track/(.*)$ /anothersite.com/order_track.php [nc,L]
http://localhost/anothersite.com/track/abc123 - Apache returns a 404.
Clearing browser cache and restarting Apache have no effect. Apache seems to "remember" the first like rewriterule used. This happens on all of my computers(Home, work, laptop)
Edit: I should have mentioned that I have an htaccess file in each directory. The root /var/www does not contain an htaccess file. Each directory's htaccess should operate independently. But they do not.
You can have this in /somesite.com/.htaccess:
RewriteEngine On
RewriteBase /somesite.com/
RewriteRule ^track/(.*)$ order_track.php [NC,L]
Then this in /anothersite.com/.htaccess:
RewriteEngine On
RewriteBase /anothersite.com/
RewriteRule ^track/(.*)$ order_track.php [NC,L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(somesite\.com|anothersite\.com)$
RewriteRule ^track/ /%1/order_track.php [NC,L]
you don't need (.*) if not needed to catch this.
Related
I'd like to redirect the following url:
http://www.example.com/food/?p=11&q=22&r=33
to
http://www.example.com/food/api.php/DBNAME/?p=11&q=22&r=33
As you may have noticed, DBNAME is the name of a database.
With the API I'm using it is necessary to include it in the URL.
Here's a brief folder structure of my program:
food/
->include/
->models/
->.htaccess
->api.php
->config.php
->test.php
I tried writing the rules on the .htaccess file but I get an Internal server error.
My .htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ api.php/DBNAME/$1 [L]
</IfModule>
I can get my head around achieving the redirection.
I also tried
RewriteRule ^food/(.*)$ api.php/DBNAME/$1 [L]
but this rule just lists the files in the directory food.
Any ideas?
Thanks!
PS: The query string is not just p=11&q=22&r=33 it may use any letter or word such as type, quantity and so on. p=11&q=22&r=33 is just an example.
Edit
From the link that #sanj provided I changed my .htaccess ffile to this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/food/?$
RewriteRule ^(.*)$ api.php/DBNAME/$1
</IfModule>
It works partially. Why partially?
I have two servers, one for testing and one for production. It works well on the testing server but not on the production server. I get an Authorization Required error. I believe it's due to the configuration in my .htaccess because if I remove the mod_rewrite module and access the url directly it works. Any ideas?
Found my mistake. The folder was protected by Basic Authentication. I needed to include login parameters in my code. I can't believe I overlooked that.
I think this will work:
<IfModule mod_rewrite.c>
RewriteBase /food
RewriteEngine on
RewriteCond %{QUERY_STRING} /DBNAME/
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^$ index.php/DBNAME/?%1 [L]
</IfModule>
Problem:
my routes not working except the root home page, I'm searching for two days to find a solution to this problem and what I found that I should change .htaccess file but solutions didn't fix any for my case, at first the url localhost/quotes/public was working well with me, but at some point I'm not sure what is it this issue showed up
what I tried:
create another route and I made sure that no routes are working only
home route, still not working except home
tried to change OverrideMode on my XAMP from None to All, didn't fix any
tried to type manually localhost/quotes/public/index.php BOOM everything
works ..
my htaccess file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
working on:
Windows 10
XAMP
Laravel 5.2.35
The problem is that your .htaccess is rewriting everything to the frontcontroller, which is normally located at {host}/index.php. In your application however it is located at {host}/quotes/public/index.php.
So you have 2 options:
1. virtual host
Set up a virtual host in your XAMPP Apache that points ie. myapp.local to htdocs/quotes/public Here is an example of how to achieve this: how to create virtual host on XAMPP. (Don't forget to add the host to your hosts file and have it point to your local macine on 127.0.0.1) You can then access your application on myapp.local/whatever-route-you-define. Alternatively you forget about XAMMP and install the homestead virtual machine, which comes preconfigured for this.
2. rewrite rule
Change you rewrite rule to rewrite all requests to quotes/public/index.php in stead of index.php. I'm no htaccess expert, but I believe it should be as simple as changing this:
RewriteRule ^ index.php [L]
to this:
RewriteRule ^ quotes/public/index.php [L]
Do note that you'll still need to access your application trough localhost/quotes/public/whatever-route-you-define which is not ideal imo. Your dev version should be as close to your live version as possible, and if you start working with absolute and relative paths and stuff in your code things will become a mess sooner rather then later.
Personally I would go for Homestead, I use it all the time and it works great once you have it running.
Btw, the reason why localhost/quotes/public/index.php is working for you right now is because RewriteCond %{REQUEST_FILENAME} !-f tells Apache not to rewrite any requests to files that actually exist (otherwise you wouldn't be able to access static assets like your css).
The .htaccess file must be at the root of the application.
Add this in this file :
RewriteEngine On
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
Assuming you haven't touched the original architecture of Laravel, and that public data is still in the same place : the public/ folder
You can also follow this good tutorial
Let me give you an example of the way I have my routes setup.
In app\Http\routes.php, here are three sample routes that I have.
Route::get('/', function () {
$values = app('App\Http\Controllers\KeywordController')->index();
dd($values);
return view('welcome');
});
Route::get('googlefile', function () {
$output = app('App\Http\Controllers\KeywordController')->printToFileGoogle();
dd($output);
});
Route::get('bingfile', function () {
$output = app('App\Http\Controllers\KeywordController')->printToFileBing();
dd($output);
});
I have WAMP setup on my environment. I have made a controller at app\Http\Controllers\KeywordController.php. If my browser is set to localhost/googlefile, then it will goto the method printToFileGoogle() in KeywordController.php.
Please try something similar to this and tell me if you get an error and if you do what error you get.
What rule should i set, to make the mod_rewrite ignore the directory "public" completely?
By that, I mean, the files should be accessible within it, but if the file does not exist, a server error page should come up with something like, FORBIDDEN, or FILE NOT FOUND what ever. I do not need custom error pages or stuff like that. I simply want the "public" to behave like there is no mod_rewrite at all.
Here is my .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
My file structure is
/system/
/application/
/public/
I want the folder public to behave, like there are no rewrite rules set at all, completely ignore it.
edit
That's my .htaccess file:
RewriteEngine on
RewriteRule ^(public)($|/) - [L,NC]
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
I already had this .htaccess in the /public/ folder:
RewriteEngine off
I've tried all the different answers above (and a ton from google). I've tried to mix 'em up what so ever.
My folders:
/system/
/application/
/public/
/public/.htaccess #RewriteEngine off
/public/favicon.ico
/index.php
Below are the url with results I'm getting:
/public/favicon.ico -> I get the favicon
/public/faviDon.ico -> I get the index.php (without mod rewrite you would get "not found")
/public/ -> I get the index.php (without mod rewrite "forbidden")
So it still does rewrite urls, if the file was not found, or upon accessing a folder directly.
Can you se it?
Thank you very much for effort guys! I really appreciate it!
EDIT
I completely setup your files on my machine
// /.htaccess
RewriteEngine on
RewriteRule ^(public)($|/) - [L,NC]
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
.htaccess in the public folder:
// /public/.htaccess
Options -Indexes
RewriteEngine off
This disables rewriting like you wanted.
/public/ -> 403 Forbidden
/public/favicon.ico -> 200 File found
/public/not-existing.ext -> 404 File not found
Do you have a index.php in you public folder?
Maybe you could remove that one..
What kind of machine your testing on?
I tested it on Linux + Apache 2 + PHP5.3
I can give you more support in the afternoon (my time +2 GMT)
EDIT 2
When I remove this line from /.htaccess is still works
RewriteRule ^(public)($|/) - [L,NC]
Everything is handled by the .htaccess in the public folder.
Maybe it's a caching problem in your browser. Try a different browser/clean up history/install app to remove cache.. (depending on what browser you're using)
I'm trying to pass requests from the root of my url to a subfolder without redirecting using mod_rewrite. (Note: I'm doing this on Rackspace Cloud Sites.)
I took the CI .htaccess, moved it up to the root folder, and changed it from this:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|public|robots\.txt)
RewriteRule ^(.*)$ /ci/index.php/$1 [L]
to this:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|public|robots\.txt)
RewriteBase /ci/
RewriteRule ^(.*)$ /ci/index.php/$1 [L]
Which gives me an internal server error. I don't believe this sort of this has an error log, which is probably why I've always considered mod_rewrite to be alchemy.
Maybe you can see what's going wrong?
Thanks!
Its not the mod_rewrite, its the server. I was able to run that no problem on my server. If you could provide a url, I can look more. But as it stands, it seems to be the server its running on.
I want to move all my web site files (even including index.php) into a subdirectory (for exp: "abc")
For example
BEFORE:
public_html
index.php
a.file
directory
an.other.file
...
AFTER:
public_html
abc_directory
index.php
a.file
directory
an.other.file
...
I want everything to work, as it was before, but i don't want to make any redirections (visible).
People should enter "http://myexmaplesite.com/directory/an.other.file/" and by .htaccess apache serve them "http://myexmaplesite.com/abc_directory/directory/an.other.file/" BUT WITHOUT EXTERNAL REDIRECTS (301,302 etc.)
How could I route all requests to a subdirectory using mod_rewrite?
Try this mod_rewrite rule in your document root:
RewriteEngine on
RewriteRule !^directory/ directory%{REQUEST_URI} [L]
Or in general:
RewriteEngine on
RewriteCond $1 !^directory/
RewriteRule ^/?(.*) directory/$1 [L]
This should be even applicable in server or virtual host configurations.
Something like
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ directory/$1 [L,QSA]