I started an app based on Angular.js and Silex for the server side.
I would use real URL (without hash) but it's not working.
I have activate pushstate on angular with the following line :
$locationProvider.html5Mode(true);
but I don't know how to configure server. Actually when I try to access to localhost/test angular did nothing but silex said : *No route found for "GET /test" ..
my htaccess is like it :
<IfModule mod_rewrite.c>
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteBase /web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php [L]
Structure of my project is Like it
|--app/
|----app.php
|----controllers/
|-------MainController.php
|--web/
|----index.php
|----js/
|---------app.js
...
Thanks you for your help
edit :
route declaration :
config(['$routeProvider, $locationProvider', function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider.when('/test',{
templateUrl : '/cuisine',
controller : 'CookController'
});
}
I am using this on my site, and its working , please give it try. Otherwise i suggest you turn on mod_rewrite logging in apache and look to logs
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ /index.html
Your problem is with RewriteBase. The rewrite base is based on URI folder and you're requesting /test not /web/test, so your rewrite base should be /
I'm just guessing but I think your document root is /path/to/your/project/web (it should be this, only the web directory is meant to be public!) so your .htaccess shouldn't have the RewriteBase on your case.
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.
For a web-application I would like to rewrite the URL.
From:
http://example.com/api/logger/all
To:
http://example.com/api/index.php/logger/all
So I tried the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/index.php/
RewriteRule ^api(.*)$ api/index.php$1 [L]
I also testet it successfully with http://htaccess.madewithlove.be/.
On the server I get a 404.
My project struct looks like following:
--api
----index.php
--htaccess
--index.html
Update:
I found a solution for external redirects.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !(index\.php)
RewriteRule ^api/(.*)$ /api/index.php/$1 [R=302]
But I need a internal redirect and if remove [R=302] I get 404 with "No input file specified." as response.
Following htaccess file works for
http://example.com/logger/all.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !(index\.php)
RewriteRule ^(.*)$ /api/index.php/$1
But adding api again results in "No input file specified."
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !(index\.php)
RewriteRule ^api(.*)$ /api/index.php/$1
mod_rewrite is a double-edged sword that is incredibly easy to misuse.
Apache 2.2.16 and later provide the extremely useful but surprisingly seldom used FallbackResource directive.
So use the following layout:
--api
----.htaccess
----index.php
--index.html
And use the following contents for your .htaccess file:
FallbackResource /api/index.php
The main difference with the mod_rewrite based solutions is that there is no substitution in the URI, so you have to parse the URI in PHP using $_SERVER['REQUEST_URI'].
For example here, if you access http://example.com/api/logger/all, your index.php file will see the following value:
echo $_SERVER['REQUEST_URI']; // prints /api/logger/all
The main interest of putting the .htaccess file in the api directory is that it ensures that only URLs under the /api prefix are handled by FallbackResource. Any URL that would cause a 404 error in the document root folder will not trigger a call to /api/index.php.
So, I've this problem:
Base Website located at http://example.com/
Second Website located at http://example.com/web2/
People making various requests to the second website like
http://example.com/myWeb/pg1 and http://example.com/web2/pg2
Recently and due to some other issues I need to have a custom new path for the second website but also keep the first one working.
The ideia is to allow users to access the second website over the two following addresses:
http://example.com/web2/
http://example.com/alternative-url-web2/
The folder /web2/ actually exists on the server, but how can I simulate the folder /alternative-url-web2/ and "redirect" the requests to /web2/?
Please note I don't want the URL on the browser to change, this must be a "silent redirect". And I also make sure that all other requests like http://example.com/other are not redirected by the second website.
Thank you.
Update:
According to #anubhava I could simply solve this issue by adding in my .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
This is probably working fine but I noticed the following:
http://ex.com/alternative-url-web2 is redirected to http://ex.com/web2/ (changing browser URL);
http://ex.com/alternative-url-web2/ is redirected to http://ex.com/(changing browser URL);
http://ex.com/alternative-url-web2/someRequest works fine and does NOT change the browser URL;
http://ex.com/alternative-url-web2/index.php works fine and does NOT change the browser URL;
Site Note:
At /web2/ there's an .htaccess that might be cause the wired redirect behavior above... So here is the file contents:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
Can the internal RewriteRule to index.php be causing all this? If yes, how can I fix it?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
Alternate code:
RewriteRule ^alternative-url-web2/?$ /web2/ [L,NC]
RewriteRule ^alternative-url-web2/(.+)$ /web2/$1 [L,NC]
This is a pretty simple rewrite. In the htaccess file in your document root, just add the following:
RewriteEngine On
RewriteRule ^alternative-url-web2/?(.*)$ /web2/$1 [L]
Unlike a redirect, which makes the browser/client send a new request for a new URL (thus changing what's in the browser's location bar), a rewrite happens entirely on the server's side.
By the way, in order to follow the trail of htaccess redirects, you could add something like this to each of them:
Header add X-Remark-Rewrite "/path.to/htaccess"
You can inspect these in the response in the developer tools.
I'm actually developping a small php website (only php, no java) and I would like to figure out something.
When I have this url called URL A: http://www.domain.com/?toto, everything's working
I would like to rewrite it to http://www.domain.com/toto (called URL B) but when I tried, I have a 404 page not found.
What do I have to use to tell apache, when you have URL B it's an alias of URL A ??
It tried proxypass, Rewrite rules without reaching my aim. Can someone help me please?
Thx a lot !!!
You need to use Apache's mod_rewrite.
A simple rewrite rule can be placed in your .htaccess file (untested):
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) /index.php?$1 [L]