Moving mod_rewrite rules from conf.d to .htaccess - php

I am having an issue with getting mod_rewrite rules working in .htaccess after moving them from the conf.d folder.
I do understand for performance sake that it would be wiser to leave it sitting there but I have folder serving php on the document root which I do not want these same rules applied to.
And here it is.. Any and all help would be greatly appreciated!
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/([^/]*)/(\d+)/? /index.php?p=$1&id=$2 [QSA,L]
RewriteRule ^/([^/]*)/([^/]*)/(\d+)/? /index.php?p=$1&s=$2&id=$3 [QSA,L]
RewriteCond %{REQUEST_URI} !/(manual|admin|awstats)
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]*)/?([^/]*)/? /index.php?p=$1&s=$2 [QSA,L]
</IfModule>
Edited with additional config:
<Location "/admin/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE site.settings
#PythonDebug Off
</Location>
Alias /adminmedia/ "/usr/lib/python2.4/site-packages/django/contrib/admin/media/"
<Directory "/usr/lib/python2.4/site-packages/django/contrib/admin/media">
Options -Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>

as astated by #Jason McCreary you can put your rewriteRules in a <Directory> section. It will be limited to that directory only. The main advantage from a Directory versus a .htaccess is that you do not need to allow anything in AllowOverride. So that's:
safer: no risk of anyone altering apache configuration by using a .htaccess (use AllowOverride None)
faster: apache does not need to search for any .htaccess in your directory or in all the parents directories (us your AllowOverride None in a <Directory /> section, start on top of the filesystem)
Now there are some difference on the rewrite Engine when used in .htaccess files, you may need to play with rewriteBase for example.

Be sure that the directory is configured with AllowOverride FileInfo (this is required for using the RewriteEngine)

Related

Rewrite to another document root

I'm working on a new website which is supposed to replace an old one. The new website will use Phalcon and it's using its own routing system. The new website will be accessible using www.mydomain.com, like the previous one. The new website DocumentRoot is /var/www/mydomain-www, the old one is instead /var/www/mydomain-platform.
Inside this "platform" there are some "web services", that many other applications (included an iOS and an Android apps) are using. Unfortunately, the new website must use these web services as well. :-(
Basically there is a PHP file for each API call and all these files are located in the root of the old website, specifically /var/www/mydomain-platform. I know it's a mess but I have to deal with that, I have inherited it. In the old website root there is also a .htaccess file that contains a rewrite route for every single API call, to the right PHP file. They look like:
RewriteRule ^ws/event/games(.*)$ /ws_events.php$1
RewriteRule ^ws/favourites/list(.*)$ /ws_teams_favorites.php$1
I need to find a way to rewrite all the url for www.mydomain.com/ws/(.*) to the directory /var/www/mydomain-platform/ws, because the web services' files are there and I don't really want to move them in the root directory of the new website.
I thought to create a symlink var/www/mydomain.www/ws pointing to the directory /var/www/mydomain-platform/ws, but I'm sure the below vhost configuration will not like it, because it will redirect everything to /var/www/mydomain/public.
I would like to put these redirect stuff inside a .htaccess, and leave the vhost like it is.
Any idea?
<VirtualHost *:80>
ServerName www.mydomain.com
ServerAdmin dev#mydomain.co.uk
DocumentRoot "/var/www/mydomain-www"
ErrorLog "${APACHE_LOG_DIR}/www.mydomain.com-error_log"
CustomLog "${APACHE_LOG_DIR}/www.mydomain.com-access_log" common
AllowEncodedSlashes On
AddDefaultCharset UTF-8
<Directory /var/www/mydomain-www>
AuthType None
AllowOverride all
Require all granted
<IfModule rewrite_module>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule ((?s).*) public/$1 [B,NE,L]
</IfModule>
</Directory>
<Directory /var/www/mydomain-www/public>
<IfModule rewrite_module>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?s).*)$ index.php?_url=/$1 [B,NE,QSA,L]
</IfModule>
</Directory>
<FilesMatch "(?i)\.php$">
Require all denied
</FilesMatch>
<FilesMatch "index\.php$">
Require all granted
</FilesMatch>
</VirtualHost>
You can move ws folder to /var/www/mydomain-www directory or create a symbolic link in the new directory. Then you need to change rule in /var/www/mydomain-www to skip anything with /ws/ in front:
<Directory /var/www/mydomain-www>
AuthType None
AllowOverride all
Require all granted
<IfModule rewrite_module>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule ^/?((?!ws2/|ws3/).*)$ public/$1 [B,NE,L,NC]
</IfModule>
</Directory>

Rewrite subdirectory url to file

I have a subdirectory called test, http://www.mywebsite.com/test/. Inside of the directory is an index.php file.
I want to put an .htaccess file inside of the test subdirectory that will rewrite the URLs so that http://www.mywesbite.com/test/paul will become http://www.mywebsite.com/test?author=paul, and so that urls like http://www.mywebsite.ocom/test/sam/3 will become http://www.mywebsite.com/test?author=sam&test=3
This is what I have so far:
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{REQUEST_URI} (/test/)
RewriteRule ^(.*)/(.*)/(.*)/?$ /test/index.php?author=$1&page=$2 [L]
RewriteCond %{REQUEST_URI} (/test/)
RewriteRule ^(.*)/(.*)/?$ /test/index.php?author=$1 [L]
Unfortunately, I get a 404 Not Found error. Am I missing something? Any thoughts are much appreciated!
EDIT: I checked phpinfo() and mod_rewrite does seem to be showing as a loaded module.
This is what my virtual host file looks like too. It looks like AllowOverride is set as well.
<VirtualHost *:80>
ServerName mywebsite.com
ServerAlias www.mywebsite.com
DocumentRoot /var/www/html/mywebsite.com/httpdocs
ErrorLog /var/log/apache2/mywebsite.com-error_log
CustomLog /var/log/apache2/mywebsite.com-access_log combined
HostnameLookups Off
UseCanonicalName Off
ServerSignature On
ScriptAlias /cgi-bin/ "/var/www/html/mywebsite.com/httpdocs/"
<Directory "/var/www/html/mywebsite.com/httpdocs/">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<IfModule mod_userdir.c>
UserDir public_html
Include /etc/apache2/mod_userdir.conf
</IfModule>
</VirtualHost>
EDIT 2: I also wanted to mention, I put some junk text in the .htaccess file to try and break it, and instead of a 404 error I got an internal server error, so it does seem like the .htaccess file is being read.
I tested Panama Jack's solution and it seems like the RewriteRules are not accounting for this already being in /test/.htaccess... so this would not work for the first example (/test/paul). Proposing an alternate solution that should work for both examples:
RewriteEngine On
RewriteBase /test/
RewriteRule ^(.+)/(.+)/?$ index.php?author=$1&page=$2 [L]
RewriteCond %{REQUEST_URI} !index\.php$
RewriteRule ^(.+)/?$ index.php?author=$1 [L]

XAMPP .htaccess mod_rewrite not working

I am running XAMPP for Windows 5.6.11. I have the following PHP file:
C:\xampp\htdocs\www.johndoe.com\index.php
which I am accessing as
http://localhost/www.johndoe.com/
As a matter of fact I need to access the following page:
http://localhost/www.johndoe.com/?value=about
as either of the following two:
http://localhost/www.johndoe.com/about/
http://localhost/www.johndoe.com/about
so I have the following in my .htaccess file:
RewriteEngine on
RewriteRule ^www\.johndoe\.com/about/?$ www.johndoe.com/?value=about
However, this is not working, as accessing the former sites gives me a 401 (not found).
Here is what I have in C:\xampp\apache\conf\httpd.conf:
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "C:/xampp/htdocs"
<Directory "C:/xampp/htdocs">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
What must I do to get my .htaccess file to be parsed and carry out the substitution I'm after?
I have tried placing the following in C:\xampp\apache\conf\httpd.conf:
<Directory />
AllowOverride all
Require all allowed
</Directory>
but have had no luck with it.
I have even tried changing my .htaccess file to the following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /www.johndoe.com/
RewriteRule ^about/?$ ?value=about
but I'm still getting the 404 not found error message.
As it turns out, with the default XAMPP configuration there is no need to C:\xampp\apache\conf\httpd.conf, hence no need to restart Apache as we are just making changes to C:\xampp\htdocs\www.johndoe.com\.htaccess. As this post on RewriteBase explains, we do not need RewriteBase since we will not use absolute paths in the destination links for .htaccess rules. Since relative links in these destination rules will be relative to the directory we are serving out of, we need delete the www.johndoe.com directory from the rule, as follows:
Place the .htaccess in ``C:\xampp\htdocs\www.johndoe.com`.
Place the following rewiterule in it:
RewriteEngine on
RewriteRule ^about/?$ index.php?value=about

Defining Document Root in Apache2/Cake PhP

I have a broken Cake PHP site that I did not write, but am charged with fixing. I have the main controller up, however no css/js/images, and when I click a link I get a 404 not found. I believe something is incorrect with mod_rewrite or the docroot config in apache.
While reading through Cake documentation, I came across this:
"app/webroot
In a production setup, this folder should serve as the document root for your application. Folders here also serve as holding places for CSS stylesheets, images, and JavaScript files."
In my /etc/apache2/sites-enabled/this-site, I see this:
DocumentRoot /u02/data/docroots/this_site
<Directory /u02/data/docroots/this_site>
Options -Indexes
AllowOverride none
Order deny,allow
Allow from all
</Directory>
So, my question:
does the above config block need to have:
/u02/data/docroots/this_site/app/webroot as the DocumentRoot and ?
Anywhere else you can think to look for troubleshooting this?
Since you have access to edit the apache config files - it's most appropriate to make a standard production install.
Fixing the docroot
does the above config block need to have: .../app/webroot as the DocumentRoot
Assuming that path exists: yes.
This will fix the broken css/js/images.
Fixing mod rewrite
Put the rewrite rules in the virtual host config:
DocumentRoot /u02/data/docroots/this_site/app/webroot
<Directory /u02/data/docroots/this_site/app/webroot>
Options -Indexes
AllowOverride none
Order deny,allow
Allow from all
// added
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</Directory>
This will then route requests for anything that's not a file to the application.
Verify that the rewrite rule used is compatible with the version of CakePHP in use, the above is based on the rule for the latest release - it changed over the years and using the wrong rewrite rule may have unexpected side effects.
What's actually broken
AllowOverride none
This prevents the .htaccess files from being read by apache. If you were to change this to AllowOverride all, and assuming the currently-ignored .htaccess files exist, the site would work - but as a development install.
Try this:
<VirtualHost *:80>
ServerAdmin xxxxxx#test.ro
ServerName test.ro
ServerAlias www.test.ro
DirectoryIndex index.php
DocumentRoot /u02/data/docroots/this_site
ErrorLog /u02/data/docroots/this_site/error.log
CustomLog /u02/data/docroots/this_site/access.log combined
<Directory "/u02/data/docroots/this_site">
Options -Indexes FollowSymLinks MultiViews
AllowOverride AuthConfig FileInfo
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
And you should have 3 .htaccess files:
/u02/data/docroots/this_site/app/webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
/u02/data/docroots/this_site/app/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
/u02/data/docroots/this_site/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Hope this helps ;)

Remove index.php in codeigniter

I know it is the most repetitive question of CI.
I am using Kubuntu and have made following changes
$config['base_url']='http://localhost/cii/';
$config['index_page'] = '';
$config['uri_protocol']= 'REQUEST_URI';
I've kept my .htaccess file where index.php is kept (in the root folder) but am getting following error:
The requested URL /cii/welcome/second was not found on this server.(404 error)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Possibly the problem is due to the AllowOverride None of document root path in apache sites-available configuration file. You need to modify the line containing AllowOverride None to read AllowOverride All in the file (/etc/apache2/sites-available/default) to make .htaccess files work as expected. Do restart apache once after made the changes.
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
Enabling use of Apache htaccess files.
I would guess that the error is because your CI is located at the subfolder /cii but the rewrite rules point to the root folder.
Try adding RewriteBase /cii/ inside the first block.

Categories