working .htaccess configs don't work on httpd.conf - macos

An early heads up - I'm a beginner student with Back-end programming and for now, even .htaccess URL rewrites was a huge pain to implement.
I have XAMPP Apache installed on my Mac (not XAMPP-VM) with a website folder called "Project" inside "/htdocs". So basically a website that I'm practicing with URL looks like this - "localhost/Project"
There was one .htaccess file in my "root" ("root" is the "/Project" folder) folder and another one inside a "PHP" folder (i.e. root/PHP/.htaccess).
Root's .htaccess had the following configs:
Options -Indexes
ErrorDocument 403 /Project/index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !(.*)Pages
RewriteRule ^(.*)$ Pages/$1.php [L,NC]
</IfModule>
Whilst root/PHP's .htaccess had this:
Deny from all
Everything worked and after reading a bit more about .htaccess best practices I wanted to move all of the above configs to httpd.conf, specifically the one located inside "/Applications/XAMPP/xamppfiles/apache2/conf". I moved the code to that httpd (properly?), commented out everything inside the previously mentioned .htaccess files, and here's how the httpd now looks like inside:
Alias /bitnami/ "/Applications/XAMPP/xamppfiles/apache2/htdocs/"
Alias /bitnami "/Applications/XAMPP/xamppfiles/apache2/htdocs"
<Directory "/Applications/XAMPP/xamppfiles/apache2/htdocs">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory "/Applications/XAMPP/xamppfiles/apache2/htdocs/Project">
Options -Indexes
ErrorDocument 403 /Project/index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !(.*)Pages
RewriteRule ^/(.*)$ /Pages/$1.php [L,NC]
</IfModule>
</Directory>
<Directory "/Applications/XAMPP/xamppfiles/apache2/htdocs/Project/PHP">
Deny from all
</Directory>
And it doesn't work. I've tried to google a solution for a while and so far completely nothing. Just in case, I'll also mention that the goal for this "CMS" project is to "write once, install anywhere".
[EDIT]
With some clarifications from #MrWhite, this is what the configs look like in xamppfiles. Also, also, Options -Indexes and /Project/PHP > Require all denied don't work as I can browse folders and access "PHP" folder from Browser. And it did not work prior to this EDIT as well.
-xamppfiles/apache2/conf/httpd.conf
Alias /bitnami/ "/Applications/XAMPP/xamppfiles/apache2/htdocs/"
Alias /bitnami "/Applications/XAMPP/xamppfiles/apache2/htdocs"
<Directory "/Applications/XAMPP/xamppfiles/apache2/htdocs">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Include "/Applications/XAMPP/xamppfiles/apache2/conf/httpd.conf"
-xamppfiles/apache2/conf/project.conf
<VirtualHost *:80>
DocumentRoot "/Applications/XAMPP/xamppfiles/apache2/htdocs/Project">
Options -Indexes
ErrorDocument 403 /Project/index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !(.*)Pages
RewriteRule ^(.*)$ Pages/$1.php [L,NC]
</IfModule>
<Directory "/Applications/XAMPP/xamppfiles/apache2/htdocs/Project/PHP">
Require all denied
</Directory>
</VirtualHost>
I'd greatly appreciate any help.

Original:
RewriteRule ^(.*)$ Pages/$1.php [L,NC]
New:
RewriteRule ^/(.*)$ /Pages/$1.php [L,NC]
You shouldn't have changed anything here. You are moving the directives from .htaccess to a <Directory> section (in the server config). These are both directory contexts and work the same way. (You are incorrectly thinking this is a server or virtualhost context, although the target URL-path would still be wrong.)
By introducing a slash at the start of the URL-path in the RewriteRule pattern (first argument) the rule will never match. If it did... the additional slash you've introduced at the start of the substitution string (second argument) would incorrectly rewrite the request to /Pages in the document root, not /Project/Pages as would seem to be the intention.
Note that if you are moving the .htaccess config to <Directory> containers in the server config then you should probably disable .htaccess overrides altogether, since a .htaccess file that contains mod_rewrite directives will completely override the <Directory> container in the server config. For example:
AllowOverride None
Note also that Order, Allow and Deny are Apache 2.2 directives and formerly deprecated on Apache 2.4. On 2.4 you should be using the equivalent Require directive instead. For example:
Require all granted
Require all denied
Extra:
Look into creating additional <VirtualHost> containers for each project, store these in separate files and include them in the main server config, rather than modifying the main server config directly. This will allow you to host multiple (separate) websites on the same webserver.

I have found the issue, which was my blind mistake.
<Directory "/Applications/XAMPP/xamppfiles/apache2/htdocs/Project">
Options -Indexes
//...
I was pointing to the wrong htdocs folder that contains my Project and the correct way was:
<Directory "/Applications/XAMPP/xamppfiles/htdocs/Project">
Hope this saves some time for anyone else that would come across this.

Related

Does Laragon ignore .htaccess?

I'm trying to move a working project from XAMPP to Laragon.
Everything is fine, but some directives I've put in my .htaccess don't work anymore: all modules seems to be enabled correctly.
What I'm trying to have working again is the versioning for my assets:
<IfModule mod_rewrite.c|rewrite_module>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.(\d+)\.(css|js)$ $1.$3 [L]
</IfModule>
This is placed in the root of the project with Laravel, but moving it into the public folder does not change anything
In the Apache config I have the following correctly uncommented:
LoadModule rewrite_module modules/mod_rewrite.so
And this is the host configuration (untouched)
<VirtualHost *:80>
DocumentRoot "C:/laragon/www/projectski/public/"
ServerName projectski.localhost
ServerAlias *.projectski.localhost
<Directory "C:/laragon/www/projectski/public/">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
As you can see it points to /public, but moving to the root (which I would like to, considering server configuration) will disable everything and turn the local site into a directory.
Please help me move to Laragon.
For those still looking for an answer, Laragon does not ignore .htaccess
The error was a simple syntax error in the IfModule
<IfModule mod_rewrite.c | rewrite_module>
# Logic
</IfModule>

How to setup base URL in codeigniter running on Ubuntu 14.04?

Installed LAMP stack. Right now I've extracted codeigniter files to /var/www/ci
but while running on browser http://localhost/ci/ the welcome page doesn't display.
Finally found the answer. Have to enable the site.
In /etc/apache2/sites-available/000-default.conf file shud modified as follows:
<VirtualHost *:80>
DocumentRoot /var/www/
ServerAdmin webmaster#localhost
<Directory /var/www/ci/>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And this worked for me! Thanks all.
If you are using ubuntu 14.04 the web server inside var/www/html, so move your folder into var/www/html/ci.
And open .htaccess file inside ci folder.paste the following code
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /ci/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
You can set the base url in /application/config/config.php on line 26.
$config['base_url'] = 'http://localhost/ci/';
If that doesn't solve the problem you have to post more informations.
Fresh codeigniter install?
No third party plugins?
No changes to routes? (in /application/config/routes.php)
No changes to the controller?
No changes to the view?
Do you get errors?
If not check if error reporting is on in the index.php (set it hard to development for testing)
Do you use Rewrite rules in an .htaccess in the /var/www/ci/ folder?
Do you autoload anything? libraries, models, languages, helpers ?
If you can't find anything, set this to 4:
$config['log_threshold'] = 4;
in /application/config/config.php on line 216.
The log file is written to /application/logs/.
Post that log, probably it helps.

.htaccess not working in xampp

.htaccess is not working in my system. I have done the following things.
Included mod_rewrite.so file in httpd.conf file
and have changed all Allowoverride None to Allowoverride All
and have restarted the server. But still it is not working. Can anyone help me?
This is my .htaccess file
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^test\.html http://www.google.com/? [R=301,L]

cakephp url rewriting not properly configured

I'm encountering a problem with my cakephp app...
I'm using Cakephp 2.4.3 with XAMPP on MacOSX Mavericks and I'm getting the message : URL rewriting is not properly configured on your server. 1) Help me configure it 2) I don't / can't use URL rewriting
But I configured the sites according to the url rewriting tutorial...
I have a Virtualhost :
<VirtualHost 127.0.0.1>
ServerName cake84.loc
DocumentRoot "/Users/me/Sites/c84/cakephp"
DirectoryIndex index.php
<Directory "/Users/me/Sites/c84/cakephp">
AllowOverride All
Allow from all
</Directory>
</VirtualHost>
with ref in hosts file : 127.0.0.1 cake84.loc
the .htaccess file is present and contains:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
and I have the file /Application/XAMPP/etc/httpd.conf with the line :
LoadModule rewrite_module modules/mod_rewrite.so uncommented...
Am I forgetting something ?!!
Notice that the same configuration was working a week ago but in the parent folder. The only change I've made was to move the whole site from : /Users/me/Sites/c84 to /Users/me/Sites/c84/cakephp and then changing the path of the virtualhost. (And for sure, restarted the apache...)
Well... Problem solved...
It seems that cakephp has problems to detect correctly if the rewrite url mode is enabled...
The best way is to call an url that should be rewritten to test the url rewritting.
In my case, the url rewriting was correctly enabled although I got this message... But unfortunately, the url I called to test it, had a syntax error (typo...) that I missed and nothing was shown...
I am also on Mavericks. For me, I had to edit the file:
/private/etc/apache2/users/myusername.conf
(Replace myusername.conf with the username on your computer.)
In this file, I had to change the lines:
Options Indexes MultiViews to Options Indexes MultiViews FollowSymLinks
and
AllowOverride None to AllowOverride All,
and then restarting Apache, solved the problem.
Hope this helps anyone else Googling for a solution on Mavericks.

How do I get rid of the need for the "/app.php/" in a new Symfony2 app's production URL?

I've done a 1st install of Symfony2,
php app/console --version
Symfony version 2.0.11 - app/dev/debug
I installed it under an existing DocumentRoot by configuring my apache2 server vhost
<VirtualHost 127.0.0.1:80>
...
DocumentRoot /srv/www/localhost_www
...
<Directory "/srv/www/localhost_www" >
AllowOverride none
Options -ExecCGI +FollowSymLinks +Includes +Indexes +MultiViews
Order Allow,Deny
Allow from All
</Directory>
...
Alias /TESTAPP "/srv/www/TESTAPP/web/"
<Directory "/srv/www/TESTAPP/web/">
Options All
AllowOverride All
Allow from All
</Directory>
Alias /TESTAPP.symfony "/srv/www/TESTAPP/"
<Directory "/srv/www/TESTAPP/">
Options All
AllowOverride All
Allow from All
</Directory>
...
After an OK install, and successfully following "The Book" to build the demo "Hello" app, if I nav in a browser to any/all of,
dev:
http://localhost/TESTAPP.symfony/web/app_dev.php/hello/ME
http://localhost/TESTAPP/app_dev.php/hello/ME
prod:
http://localhost/TESTAPP.symfony/web/app.php/hello/ME
http://localhost/TESTAPP/app.php/hello/ME
I see the expected,
"Hello ME!"
I want to rewrite only the production URLs, hiding "app.php" so that the following set of URLs work in the nav bar of a browser for my Symfony2 app:
dev:
http://localhost/TESTAPP.symfony/web/app_dev.php/hello/ME
http://localhost/TESTAPP/app_dev.php/hello/ME
prod:
!! http://localhost/TESTAPP.symfony/web/hello/ME
!! http://localhost/TESTAPP/hello/ME
Iiuc, the default web/.htaccess
cat web/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
needs to change.
I've dug around in the docs and on the web, tried vdiffferent recommendations for modding the RewriteRule, adding 'RedirectMatch permanent', etc., and I'm not managing to get a simple
http://localhost/TESTAPP/hello/ME
to ever work. I always seem to need to add the "/app.php/".
What are the specific changes I'd need to make in order to get this to work?
Thanks.
#chasen
is mod_rewrite enabled for apache?
yes
if you have your .htaccess file in your web directory
i do
and you host file pointing to the web folder the
it is, as above,
Alias /TESTAPP "/srv/www/TESTAPP/web/"
<Directory "/srv/www/TESTAPP/web/">
app.php should not be needed.
that's my problem. without it, it's not working for me.
Specifically,
https://localhost/TESTAPP/app.php/hello/ME
returns:
"Hello Me!"
but
https://localhost/TESTAPP/hello/ME
returns
Object not found!
The requested URL was not found on this server.
If you entered the URL manually please check your
spelling and try again.
If you think this is a server error, please contact
the webmaster.
Error 404
i know that its a silly question but is mod_rewrite enabled for apache?
if you have your .htaccess file in your web directory and you host file pointing to the web folder the app.php should not be needed.
if you are running ubuntu or a similar distro you can do:
sudo a2enmod rewrite
sudo service apache2 reload
sudo service apache2 restart
That should enable the mod, and reload apache.
EDIT:
That response you are getting back is it in a symfony page? if it is its likely that your prod cache needs to be cleared, from the root directory for the application use the symfony console to clear the cache:
php app/console cache:clear --env="prod"
EDIT:
If neither of those have worked you may try adding a RewriteBase to the htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /TESTAPP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
In my case I need to modify the line containing AllowOverride None to read AllowOverride All. This tells Apache that it's okay to allow .htaccess files to over-ride previous directives. You must reload Apache before this change will have an effect (https://help.ubuntu.com/community/EnablingUseOfApacheHtaccessFiles):
Do this: - go to "/etc/apache2" modify file "apache2.conf" section:
<Directory /var/www/>
Options FollowSymLinks
AllowOverride All
Require all granted
restart server: /etc/apache2# service apache2 restart
it should work. If it does not work, still you have done 1 step right to insure .htaccess is allowed to override. There are many other reasons can be fixed with the above instruction.
Adding the RewriteBase rule with the name of the folder in which the project is held to the .htaccess file does it's job! Thanks!
RewriteBase /project_name

Resources