.htaccess rewrite rules misbehaving - .htaccess

ErrorDocument 404 /pageNotFound.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^profile/([0-9a-zA-Z-_]+)/message/?$ message.php?user=$1 [NC,L]
#
#RewriteRule ^profile/([0-9a-zA-Z-_]+)/([0-9a-zA-Z-_]+)/?$ profile.php?user=$1&content=$2 [NC,L]
#
RewriteRule ^profile/([0-9a-zA-Z]+)/?$ profile.php?u=$1 [NC,L]
#
RewriteRule ^story/([0-9a-zA-Z-_]+)/?$ blog_post.php?url=$1 [NC,L]
#
RewriteRule ^forums/questions/([0-9a-zA-Z-_]+)/edit/?$ question_edit.php?url=$1 [NC,L]
#
RewriteRule ^forums/questions/([0-9a-zA-Z-_]+)/?$ question.php?url=$1 [NC,L]
#
RewriteRule ^forums/contribution/([0-9a-zA-Z-_]+)/edit/?$ contribution_edit.php?url=$1 [NC,L]
#
RewriteRule ^forums/contribution/([0-9a-zA-Z-_]+)/?$ contribution.php?url=$1 [NC,L]
#
RewriteRule ^forums/tags/([a-z-]+)/?$ allTags.php?tag=$1 [NC,L]
#
Above is my .htaccess file and I am using WAMP.
At first when I did this, everything works fine, but later on, some URLs started misbehaving for example if I type in localhost/profile/debbie after the page finishes loading the url changes to localhost/profile/Debbie/?u=debbie, though the page contents work fine. I just don't know why. But if I do something like localhost/profile/the_king the url remains the same after loading.
But later on the if I do this localhost/profile/the_king I started getting an error message of undefined index u
Secondly I have a Subdomain under WAMP for my mobile site which uses an IP address of 192.168.106.1 and I use a duplicate of the above .htaccess file for it, but if I do this 192.168.106.1/profile/the_king it just get back my pageNotFound.php error page back.
This is really getting me angry because I watched some video tutorials and I did exactly what they did but I just don't know why mine is misbehaving
Edit here is my configuration for my Subdomain
# Tells Apache to identify which site by name
NameVirtualHost *:80
# Tells Apache to serve the default WAMP Server page to "localhost"
<VirtualHost 127.0.0.1>
ServerName localhost
DocumentRoot "C:/wamp/www/"
</VirtualHost>
# Tells Apache to serve Client 1's pages to "client1.localhost"
# Duplicate and modify this block to add another client
<VirtualHost 127.0.0.1>
# The name to respond to
ServerName localhost
# Folder where the files live
DocumentRoot "C:/wamp/www/mobile/"
# A few helpful settings...
<Directory "C:/wamp/www/mobile/">
Allow from all
Order Allow,Deny
# Enables .htaccess files for this site
AllowOverride All
</Directory>
# Apache will look for these two files, in this order, if no file is specified in the URL
DirectoryIndex index.html index.php
</VirtualHost>
<VirtualHost 192.168.56.1>
# The name to respond to
ServerName localhost
# Folder where the files live
DocumentRoot "C:/wamp/www/"
# A few helpful settings...
<Directory "C:/wamp/www/">
Allow from all
Order Allow,Deny
# Enables .htaccess files for this site
AllowOverride All
</Directory>
# Apache will look for these two files, in this order, if no file is specified in the URL
DirectoryIndex index.html index.php
</VirtualHost>
<VirtualHost 192.168.106.1>
# The name to respond to
ServerName m.localhost
# Folder where the files live
DocumentRoot "C:/wamp/www/mobile/"
# A few helpful settings...
<Directory "C:/wamp/www/mobile/">
Allow from all
Order Allow,Deny
# Enables .htaccess files for this site
AllowOverride All
</Directory>
# Apache will look for these two files, in this order, if no file is specified in the URL
DirectoryIndex index.html index.php
</VirtualHost>

Related

Codeigniter 4 Multisite

I have installed Codeigniter 4 on Raspbian and everything seems to be working fine.
My web directory is /var/www/html
Inside there are two folders containing two different Codeigniter-4 apps that I would like to invoke with:
blue.ddns.net -> /var/www/html/blue/public/index.php
black.ddns.net -> /var/www/html/black/public/index.php
So I'm creating a .htaccess file to put in the folder /var/www/html/ to handle the two requests
Well, I'm still at step 0 because I can't get the .htaccess file to work properly.
Below I attach a copy of the file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /var/www/html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ blue/public/index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
The Rewrite module is activated
the error that appears in /var/log/apache2/error.log is:
/var/www/html/.htaccess: Expected </IfModule> before end of configuration, referer: https://blue.ddns.net/
That shouldn't really be done in your htaccess. You should setup that in your apache virtual hosts.
Go into your sites-avaiable folder.
$ cd /etc/apache2/sites-available
Create a new virtual host called blue.ddns.net.conf
$ touch blue.ddns.net.conf
Open that file with nano or any other text editor you might like and add the following.
<VirtualHost *:80>
ServerAdmin your#email.com
DocumentRoot /var/www/html/blue/public
ServerName blue.ddns.net
ServerAlias blue.ddns.net
<Directory "/var/www/html/blue/public">
allow from all
AllowOverride All
Options None
Require all granted
</Directory>
RewriteEngine on
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteCond %{SERVER_NAME} =blue.ddns.net
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Save and close the file.
Then add this to your apache with the following:
$ sudo micro blue.ddns.net.conf
Repeat the same process for the other codeigniter install and reboot apache.
$ sudo service apache2 restart
That should do it.
Now if you want to override some config that would be something that you might do in your htaccess file for each codeigniter 4 install.
I've always worked in hosting and never had to do Apache configurations.
But now I'm discovering the Raspberry/Linux world.
Thanks to your answers I was able to better understand the problem and solve it (for the moment) in the following way:
Now my configuration files 000-default.conf (port 80) and 000-default-le-ssl.conf (port 443) both contain the following code:
<Directory "/var/www/html">.
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
in /var/www/html instead I created a .htaccess file with the following code:
# Disable directory browsing
Options All -Indexes
<IfModule mod_rewrite.c>.
RewriteEngine On
RewriteCond "%{HTTP_HOST}" "black\.ddns\.net"
RewriteRule ^(.*)$ black/public/index.php?/$1 [L]
RewriteCond "%{HTTP_HOST}" "blue\.ddns\.net"
RewriteRule ^(.*)$ blue/public/index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
apps are located in
/var/www/html/blue
and
/var/www/html/black
It's a very simple solution that serves my purposes.
Although I think it's not very elegant
What do you think about it?

Symfony4 routing inside a subfolder

I have a symfony app in my /var/www/html/app/ and I'd like to access it via host/app/ but I can't make it work.
I have a conf file with this
<Directory /var/www/html/app>
AllowOverride All
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
</Directory>
It's commented because I tried to do the redirect here but it didn't worked so I made the following .htaccess.
And this htaccess in /var/www/html/app/.htaccess
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
With this .htaccess, When I go to ://host/app I go to the symfony app (not the apache directory structure) and that seems to be a good start.
But the problem is I'm getting an Exception No Routes found for /app/.
That's normal because /app/ is the subfolder in my server, not the start of symfony routes.
How can I make the symfony routes beginning after the /app/ folder path ?
I know there is some questions about symfony2/3 routing in subfolder but the answer requires to separate src and public files, that's not what I'd like to have.
I just want my routes working when accessing ://host/app.
For information, It works great when I access ://host/app/public/*Symfony routes*
Edit:
New app.conf
Alias /rapp "/var/www/html/app/public"
<Directory /var/www/html/app>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
#RewriteBase /app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!public/index.php)$ public/index.php$1 [L]
</IfModule>
</Directory>
I managed to resolve the problem !!!
I'm not sure the config is "proper" though but it seems to work.
Here is my working app.conf file (in /etc/apache2/sties-enabled/app.conf)
Alias /app "/var/www/html/app/public"
<Directory /var/www/html/app>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteRule (/public) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
</Directory>
The first RewriteRule is to prevent from internal Rewrite loop when I rewrite url like example.com/app/page-1 to example.com/app/public/index.php/page-1.
As page-1 is not a directory or a file it will loop infinitely.
So I assume as soon as I have a /public part, the rewrite worked and we stop the rewrite by doing nothing and end the rewrite with the [L] flag.
I'm not suire why I had to remove the public/ part in the "final" RewriteRule (RewriteRule ^(.*)$ index.php/$1 [L]) though.
If someone have ideas to improve this config, feel free to leave comment.
The problem is the that your web server's document root (the directory that apache will make public via the url) and your project's document root (the public directory) are not the same. Instead your server points to the project directory. That is why you have to manually go into the project's document root by adding the public/ to the url.
You should avoid making the project directory accessible as this could expose your configuration and thus make your website vulnerable, e.g. by giving people access to your database through the credentials inside the config.
You might be able to achieve your goal by storing the project somewhere else and then only symlink your project's public dir to /var/www/html/app. This will probably require some changes to your apache config, e.g. adding Options FollowSymLinks.
Another, more elaborate, approach could be using mod_proxy. The configuration will roughly look like this:
# Your public server reachable from the outside
<VirtualHost *:80>
ProxyPass /app http://127.0.0.1:8080/
</VirtualHost>
# Your internal server where the requests are proxied to
<VirtualHost 127.0.0.1:8080>
<Directory /var/www/html/app/public>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
...
</VirtualHost>
As you can see the internal web server points to your project's document root (public dir). You might have to do additional both in the apache setup and app, e.g. make sure trusted proxies are set correctly or adding ProxyPreserveHost On to the apache setup, but roughly this should point you in the right direction.
edit: I missed another obvious solution, using mod_alias to point your symfony app to /app:
Alias /app /var/www/html/app/public

Rewriting from public_html to root

I try to read many of the answer regarding htaccess but I could not find my way out.
Image this architecture in webhosting: root/public_html/subfolder
Now with the following code in htaccess file (placed in public_html) I can manage that when the domain is type in browser the visitor land in subfolder without digit example.com/subfolder, example.com is enough. It works.
What I would like now is that the visitor will be redirect not in subfolder but in an "extrafolder" placed inside the root (level root that is the same level where the folder public_html is placed but not inside public_html).
So how to modify this code:
# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Change example.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/subfolder/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /subfolder/$1
# Change example.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ /subfolder/index.html [L]
I try changing the /subfolder/ with ../extrafolder/ (supposing the double dot will push up one level) but does not work.
Any idea?
Thank you
The current configuration you put is just working in case you put .htaccess inside the root folder of your website, which in this case is public_html, so no way to access another folder outside that root, except if you have directory alias.
In case you have access to change your apache configurations
You need to have the following steps to completely control the root of your website.
1- Yur apache virtual host configuration (this should be in /etc/apache2/sites-enabled/example.conf) should be something like the following:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/test
<Directory /var/www/test/>
DirectoryIndex index.php
AllowOverride All
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
3- You need to add this Alias /externalfolder/ "/var/www/test/externalfolder/" to the virtual host configuration above
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/test/public_html
Alias /externalfolder/ "/var/www/test/externalfolder/"
<Directory /var/www/test/public_html>
DirectoryIndex index.php
AllowOverride All
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Now you can access that via http://example.com/externalfolder/file.php

Codeigniter doesn't work without index.php

I'm using MAMP with alias named works.
Codeigniter is at localhost/works/project/web
controllers doesn't work without index.php before them (localhost/works/project/web/index.php/auth/register)
$config['base_url'] = 'localhost/works/project/web/'; //with http://
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
(I tried all of them for uri_protocol)
I created and edit .htaccess file at /User/me/works/project/web/
I tired all the .htaccess files about this issue.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Users/me/works/project/web
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#‘system’ can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn’t true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
#Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don’t have mod_rewrite installed, all 404’s
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
creates error, which is:
[error] [client ::1] File does not exist: /Users/me/works/project/web/auth
it is same when I use RewriteBase /
mod_rewrite is active in phpInfo.
I couldn't find the solution.
Your RewriteBase is wrong. It should not be the relative file path for your server, it should be the relative URL path for your rewrite rules and such. More info here.
If you've ever used HTML's <base> tag, it's pretty much the same principle. If your main localhost URL is http://localhost, and your project is in the subfolder http://localhost/my-project, then you would use RewriteBase /my-project/.
If you didn't use RewriteBase, and your project is in a subfolder, you'd have to add the subfolder to every one of your URLs and rewrites, like this:
RewriteRule ^(.*)$ /my-project/index.php/$1 [L]
Food for thought:
Start your .htaccess files as simple as possible. The more you throw at it right away, the more that can go wrong, and the harder it is to debug. Especially if you're a noob to .htaccess files. Don't blindly copy and paste -- figure out what things actually do.
The system folder access part is redundant now -- the system folder has its own .htaccess to restrict requests, just like the application folder.
1.Make below changes in application/config.php file
$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
2.use this in .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
and enable rewrite mode using below command
a2enmod rewrite
and Edit the file /etc/apache2/sites-enabled/000-default
change the AllowOverride None to AllowOverride All.
and finally Restart your server
You certainly don't have mod_rewrite on or you did not install it with MAMP
Open your httpd config file and change
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
To
AllowOverride All
uncomment this line:
#LoadModule rewrite_module libexec/apache2/mod_rewrite.so
Also make some changes to users here: /etc/apache2/users/username.conf
LoadModule php5_module libexec/apache2/libphp5.so
DocumentRoot "/Users/username/where/ever/you/like"
<Directory "/Users/username/where/ever/you/like">
Options Indexes MultiViews +FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Get more info here http://adam.merrifield.ca/2010/08/09/apache-mod_rewrite-php-and-os-x-10-6-snow-leopard/
Restart MAMP
To find out if mod_rewrite is on, follow this answer in Stackoverflow:
How to check if mod_rewrite is enabled in php?
Just to be sure your .htaccess is not the issue, could you try this one. It's been tested:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /Users/me/works/project/web/
# If the start of the URL doesn't match one of these...
RewriteCond $1 !^(index\.php|robots\.txt|assets|cache|themes|uploads|css|images|js)
# Route through index.php
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Access to certain folders are granted above and the rewritebase has a trailing slash.
Make sure you have any other nested .htaccess overriding this very one.
I fixed the problem by instead of using .htacces, I write them to httpd.conf.
I checked the AllowOverride is All but I'm not sure why .htaccess isn't working. Because of I lost too much time with this issue, I'm going to use this way.
Thanks for the answers.
Use AllowOverride All on your vhost:
<Directory "/path/to/your/site">
....
AllowOverride All
</Directory>
Open your httpd config file
LoadModule rewrite_module modules/mod_rewrite.so then AllowOverride All
<Directory "C:/xampp/htdocs"> //example for xampp
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I have installed codeigniter in a url like the format: http://11.12.34.45/~project/
I tried different suggestions and finally this following url provided the solution:
https://ellislab.com/expressionengine/user-guide/urls/remove_index.php.html
You need to change two files of Apache,
httpd.conf
httpd-vhosts.conf
change this in httpd.conf
<Files ".ht*">
Require all denied
</Files>
to,
<Files ".ht*">
Require all granted
</Files>
and add below code in httpd-vhosts.conf,
<VirtualHost *:8000>
ServerName localhost
DocumentRoot c:/wamp64/www
<Directory "c:/wamp64/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Allow from all
</Directory>
</VirtualHost>

How to get .htaccess files working with Apache VirtualHost

My problem is that my .htaccess file on my local server is not being read. The settings in the VirtualHost file seem to always take precedence.
I have tried the following:
Enabled mod_rewrite
Changed the AllowOverride to All but this causes a HTTP Error 500 Internal server error. I have tried it with various options but it always causes a 500 error.
I am using a VirtualHost file on Ubuntu which looks like the following:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /web/website
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /web/website>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</VirtualHost>
In my .htaccess file under /web/website I have the following rules (which are not being read):
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTP_USER_AGENT} ^facebookexternalhit
RewriteRule ^(.*)$ ogtags.php?$1 [L,QSA]
ErrorDocument 404 /404
ErrorDocument 401 /401
One thing I tried which did work was appending these rules directly into the VirtualHost file, but I would like my .htaccess file to work! Is that such a big ask? :(
Edit: So I looked in my apache error.log and it says Invalid command 'Action', perhaps misspelled or defined by a module not included in the server configuration referring to my .htaccess file. There doesn't seem to be a module called Action which I can enable. Any ideas?
Edit 2: I noticed that my httpd.conf file is blank. Should this matter since I am using VirtualHost files?
After looking at my apache error.log I realised I just had to enable the Apache actions module:
sudo a2enmod actions
And no more 500 Internal Server Errors!
Hope this helps somebody down the line :)
You miss the RewriteBase / directive
Add it after the RewriteEngine directive :
RewriteEngine On
RewriteBase /

Resources