I have a REST API that previously had urls on my localhost that looked like:
localhost/api/v1/collection/resource
As I'm working on an API and web app locally, I implemented apache virtual hosts to get an api url of:
api.mydomain.com/v1/collection/resource
I have a front controller that I'm routing calls. api.mydomain.com hits the front controller successfully. api.mydomain.com/v1 lists the directory (all the php files), which is a problem, and api.mydomain.com/collection/resource results in a 404.
My directory structure is:
/api
/v1
php files
.htaccess
Router.php
index.php
.htaccess contains:
RewriteEngine On
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
I'm not quite sure where the issue lies. Anything other than the api.mydomain.com seems to bypass hitting index.php. I'm not too familiar with these rewrites, so any insight is appreciated.
Try this
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^api.mydomain.com$ [NC]
RewriteRule ^(.*)$ index.php [R=301,NC,L]
Found the issue. When I created the vHosts I copied an incorrect setting of AllowOverride None, which was rendering my htaccess files ineffective. Changing to AllowOverride All under the associated vHost directory resolved this.
Related
I am currently following this guide to allow the running of our current legacy PHP application inside of Laravel with a view to migrating over totally over time.
https://tighten.co/blog/converting-a-legacy-app-to-laravel/
To this end I have managed to get Laravel to run the legacy application index.php file however I believe there may be some issue with the routing as the URL is not correct and the login page does not load.
Here is my current file structure, I have two .htaccess files one at the root of laravel and one in the root of the legacy folder.
The laravel root htaccess file is rewriting the public folder so it should not show in the URL while working on dev.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
The legacy applications .htaccess file handles all the routing for the views and files of the old system, see below.
Options -Indexes
DirectoryIndex index.php
AddType image/svg+xml svg
AddType image/svg+xml svgz
# HSTS Header - removed https only as AWS lb > server is http
Header set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
# core redirects
RewriteEngine On
RewriteBase /legacy
RewriteRule ^login/?$ /views/login/index.php [QSA,L]
RewriteRule ^app-redirect?$ /callback/apps/redirect_device.php [QSA,L]
RewriteRule ^login/logout?$ /views/login/logout.php [QSA,L]
RewriteRule ^register([/]*)$ /views/login/register.php [QSA,L]
RewriteRule ^login/change-password([/]*)$ /views/login/change_password.php [QSA,L]
RewriteRule ^authenticate/auth-check([/]*)$ /views/login/auth_check.php [QSA,L]
It also appears to be affecting the public URL rewrite when accessing the application as it adds public back into the URL like so:
system.co.uk/public/login
when it should be
system.co.uk/login
Summary:
So I have it successfully running the index.php file located in the legacy folder however it just stops there it doesn't appear to be taking any of the routes in the legacy .htaccess file like /login into the account.
Does anyone have any idea what could be going on here?
the htaccess file under the legacy folder shouldn't affect the root folder.
redirects can be cached by your browser, have you tried other browser or used incognito mode?
I currently have a problem with rewriting my URL's using the .htaccess file when a Laravel project is in a subdirectory.
usually when not in subdirectory having /vacancies -> /index.php/vacancies using this below .htcaccess file works.
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine on
RewriteLog "/var/log/httpd/rewrite.log"
RewriteLogLevel 3
# 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}]
Though now the project exists within a subdirectory I require the URL to be re written as so /vacancies -> /abc/index.php/vacancies.
The home page works correctly though any links just return a not found error.
What changes would I need to have the .htaccess file do this for me.
so on investigating further upon entering 'index.php' into the url itself the page loads correctly if this helps anymore with answering the question.
Thanks!
I wouldn't modify the default .htaccess provided in a Laravel app. You need to use Apache's directory alias in the virtual host config file for your site. You need to have the following format:
#Change the paths accordingly
Alias /vacancies /path/to/app/public
<Directory /path/to/app/public>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted #If using Apache 2.4 add this.
</Directory>
I managed to fix this thank you for your suggestions, This turned out to be a problem with the virtual host file and where the project was created as I have soft links let up to direct apache.
As such Apache was looking in the actual project location set in the virtual hosts file rather than the soft link one intended causing the not found issues.
Check of .ht* permissions in the apache httpd config file if you are using apache. I had the same problem and I had permissions denied problem for .htaccess file. If you are on shared host, try to contact hosting support.
Tried putting it on top of the htaccess file ? Helps for me in some kind of cases, seems weird but it does.
I notice fro your answer you've managed to "fix" this. However, you have some fundamental errors in the .htaccess file you posted, so I'm not sure how this is working exactly?
RewriteLog "/var/log/httpd/rewrite.log"
RewriteLogLevel 3
These two directives are not valid in .htaccess files and consequently will result in a 500 Internal Server Error. These can only be used directly in the server config or virtual host context. These are also Apache 2.2 directives, so won't work on Apache 2.4.
RewriteRule ^index.php [L]
(Part of the "front-controller"). You are missing a space between the first and second arguments, so this will fail to rewrite any requests to index.php - your "front-controller". (Maybe this is just a typo, but it's difficult to see a typo like this could creep in?) For example, this should be more like:
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
These directives should be before the front-controller, not at the end of the file. Otherwise, they are not going to execute for any request that is routed through your framework.
when not in subdirectory having /vacancies -> /index.php/vacancies using this below .htcaccess file works
That's not actually what the above .htaccess code does. It should simply be rewriting the request to /index.php. (The frawework/Laravel then looks at the full URL that was requested.) If it rewrote the URL to /index.php/vacancies (that some frameworks do) then you would need to read the URL using pathname information (PATH_INFO).
Though now the project exists within a subdirectory I require the URL to be re written as so /vacancies -> /abc/index.php/vacancies.
See above regarding the PATH_INFO. But if the .htaccess file is located in the document root of the site and the /abc subdirectory should be entirely hidden then you still have some work to do. You'll need to set the appropriate RewriteBase and modify the condition that removes the trailing slash off non-directories.
Otherwise, if the /abc directory is part of the URL then the .htaccess file can be moved to the /abc subdirectory, but you'll still need to modify the directives that remove the trailing slash.
I have a webapp written in Laravel that needs to run in a folder on a web host.
The app will have to be accessible via hostname.com/webhit/. This will point to the app's home page.
I only have one route:
Route::controller('/', 'HomeController');
HomeController's getIndex needs to serve the home page. This works.
However, as soon as I want to go to something like hostname.com/webhit/login, I get a 404 from Apache.
Obviously, .htaccess is not working properly. I need it to, essentially, turn URLs that look like hostname.com/webhit/login into hostname.com/webhit/index.php/login.
I have a .htaccess file in www/webhit (where index.php is located) that looks like this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ webhit/index.php/$1 [L]
</IfModule>
I am not very familiar with .htaccess file syntax, but I believe it's doing something wrong.
Edit:
I did it. My .htaccess was wrong (it actually causes a redirect loop), but the issue was that it wasn't even being parsed by Apache (hence the 404 instead of a 500 due to >10 redirects in a request). I did the following steps in order to get everything to work:
Enable mod_rewrite and restart Apache (plenty of docs out there on how to do this)
But wait, there's more! By default, Apache on Ubuntu prevents URL rewrites. See this site. Most importantly, the following fragment from the URL above is very important: "By default, Ubuntu's Apache will ignore the directives in your .htaccess files." You will need to actually enable rewrites by editing \etc\apache2\sites-available\default and setting AllowOverride to all (see link above for more details).
Reload the configuration (or just restart apache).
Make sure you're using the correct .htaccess. My original version actually has a redirect loop in it. See the selected response for the correct version.
After this, I got it to work. I hope it helps future programmers having a similar issue!
Check default server requirements - laravel .htaccess file works for most situations. Try with this:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I would suggest you to use resourceful controllers - mappings from your route to your controllers methods are much more clear, and you'll get full
resource with one command (routes,models,views,controllers)
I am running wamp on win7 just in case. I am trying to route all requests through a route.php to get clean URLs. Below is my htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !(auth|folder2) [NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*) ./route.php?path=$1 [L]
As you can see the rule should apply to all files, and it did when I ran it on my old wamp installation. Now after copying the files to a new system, for some reason, the rule seems to work for all links except index.php. So if I try http://localhost/proj, I get no routing in the new system. Any ideas?
And before anyone asks, yes rewrite_module is on, and the htaccess is being read and executed cause it works for all other links like http://localhost/proj/users and also because if I try putting garbage values in the htaccess it throws a nice 'internal server error'.
I'm guessing you have an index.php in that folder, and this condition:
RewriteCond %{SCRIPT_FILENAME} !-f
...tells apache to perform the rewrite only if the file does not exist.
I would suggest or renaming the file, as you'll need that condition to route static assets like images, scripts, etc.
If there is no index.php (or index.htm[l]) in that folder, just define route.php as the default file in that directory. In your .htaccess:
DirectoryIndex route.php index.php index.html index.htm
(Be aware that this will also apply to any sub-directories as well)
I've installed Xampp as a web server and want to make clean URLs for one of my projects. So I put these simple lines in .htaccess file in the root of this project:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Also I made links in this project like this:
About Us
I expected to go to localhost/this_project/about/, but it does not work. I go to localhost/about/ and this is wrong. How can I solve this problem?
I also have put this line in the 2nd line of .htaccess file, but no change happened.
RewriteBase /this_project/
Attention: There are many directories in htdocs folder for each project.
You htaccess looks OK so I guess you are missing
AllowOverride All
In the VHOST <directory> block. If you server will fail after you add AllowOverride make sure you enabled mod_rewrite.