Apache Rewrite Rule httpd-vhosts.conf - apache

I have a VHOST with the following rule
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^.*$ /router.php [NC,QSA,L]
Pretty much just makes every URL go through an advanced routing system
However, its becoming a conflict when trying to set up my news system using WordPress.
All I need help with is creating a new rewrite rule to put all through the router with the exception of one directory, for example named "wordpress."
Its all on my local machine, but here is the entire VHOST config
<VirtualHost *:80>
DocumentRoot "/Users/tyler/Documents/mysite"
ServerName mysite.local
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^.*$ /router.php [NC,QSA,L]
<Directory "/Users/tyler/Documents/mysite">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

All I need help with is creating a new rewrite rule to put all through
the router with the exception of one directory, for example named
"wordpress."
Based on that criteria, you should add the following rule to the mix:
RewriteCond %{REQUEST_URI} !^/wordpress
Which will make the whole ruleset now be as follows:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/wordpress
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^.*$ /router.php [NC,QSA,L]

Related

Rewrite rule to return index.html in all subdirectories without change the url

I Have the next configuration
<VirtualHost *:443>
ServerName projectsite.com
ServerAlias projectsite.com
DocumentRoot /var/www/project
<Directory />
RewriteEngine On
Options +FollowSymlinks
AllowOverride All
Require all grant
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/?.+$ / [L]
</Directory>
</VirtualHost>
The project manage all urls from the client, so, I only need to return the index.html file and I render the modules with javascript. It works ok but If the url matches with a folder then I am getting a 403 error "Forbidden". Instead of this, I need return the root index.html file either is the url matches with a directory name or not.
¿How can I do it ? I tried to find on internet but I didn't found something similar.
UPDATED:
how Dusan Bajic told me, I removed that line RewriteCond %{REQUEST_FILENAME} !-d and it resolved the first problem, but now the Rule is adding a slash at the final of the url,
If I have "site.com/reports" it changes to "site.com/reports/". I need to mantain the url without this slash. I think the problem is with the
next Rule:
RewriteRule ^/?.+$ / [L]
But i don't know the correct way to make it
How #dusanBajic commented I had a rule that prevented what I need.
The rule is :
RewriteCond %{REQUEST_FILENAME} !-d
This rule indicates that the rewrite rule only apply if the folder does not exists. So what I did was Remove that instruction and add the next rule DirectorySlash Off to prevent apache puts a slash at the final of the url.
The code left like this:
<Directory />
RewriteEngine On
Options +FollowSymlinks
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/?.+$ / [L]
</Directory>

Redirect if specific URL is given with htaccess

I have a very straightforward url friendly htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(\w+)/?$ index.php?id=$1
So domain.com/something is mapped to domain.com/index.php?id=something.
Every URL can have an additional parameter, so for example domain.com/something can be in the form of domain.com/something?custom_value
Now I need to address this particular situation: If a particular ID is matched, then a custom value must be forced. So, if I go to domain.com/somethingelse I want to be redirected to domain.com/somethingelse?custom_value
I've tried different rules with no luck. This was my last attempt but I get a message that the server is making a rediretion that will never be completed.
RedirectMatch 301 ^/(somethingelse)/?$ /somethingelse?custom_value
Another attempt was this, resulting in a 500 Internal Server Error
RedirectMatch 301 ^/(somethingelse)/?$ somethingelse?custom_value
complete htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RedirectMatch 301 ^/(somethingelse)/?$ somethingelse?custom_value
RewriteRule ^(\w+)/?$ index.php?id=$1
EDIT 1:
Well, apparently this rule works (not always) on local machine, but no online server
RedirectMatch 301 ^somethingelse$ somethingelse?custom_value
EDIT 2:
I've also tried (also works on local machine, but no online server)
RewriteRule ^/somethingelse$ /somethingelse?test [L,R=301]
EDIT 3
These are my virtualhosts:
local virtual host
<VirtualHost vhostname>
DocumentRoot /var/www/path/to/project
<Directory /var/www/path/to/project>
AllowOverride All
</Directory>
</VirtualHost>
server virtual host
<VirtualHost *:80>
ServerName subdomain.domain.com
DocumentRoot /var/www/path/to/project
<Directory /var/www/path/to/project
AllowOverride All
</Directory>
</VirtualHost>
You may use these rules in your site root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^somethingelse/?$ %{REQUEST_URI}?custom_value [R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\w+)/?$ index.php?id=$1 [L,QSA]
Make sure to test this in a new browser or test it after completely clearing your browser cache.

Infinite loop with mod_rewrite and vhost_alias

I made myself a dynamic vhost file for all directories in /home/lukasz/websites
<VirtualHost *:80>
ServerName vm01.dev
ServerAlias *.vm01.dev
SetEnv APPLICATION_ENV "lukasz"
VirtualDocumentRoot /home/lukasz/websites/%1/public
<directory /home/lukasz/websites/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
</directory>
</VirtualHost>
It works well until very simple rule from mod_rewrite comes to play. If I add following code to the .htacesss
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
The apache server will be unable to respond. It will go into an infinite loop and will break out from the execution after 10 redirects. This problem is tightly related to VirtualDocumentRoot I'm using. How should I modify my config to keep it dynamic and stop it breaking with about htaccess?
I'm guessing you want to make index.php the default page, when the requested resource doesn't exist, you didn't say.
If this is the case then adding an ErrorDocument entry to your virtualHost will suffice e.g.
ErrorDocument 404 /index.php
If you are going to use mod_rewrite, it's far more efficient to place the rules in the Virtualhost definition than in a .htaccess file. As the httpd.conf is read once at start-up and all rule compiled, as opposed to the .htaccess file being read and all rules parsed for each request. Anyway the following should stop the loop:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]
Assuming you have an: /index.php
FYI: The Apache mod_rewrite documentation is worth a read if your unclear as to what the above rules do, or if you want something a little more abstract consider the following post.

htaccess, one line does not work

I am using this htaccess file. Everything is working fine except that first RewriteRule.
When I open localhost/music/test/ I get a 404 not found error.
When I open localhost/music/ or localhost/music/a/b/etc/ it works like it should.
Anyone knows what I am doing wrong here?
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^([^/]*)/$ /music/index.php?id=$1 [NC,L,QSA]
RewriteRule ^([^/]*)/([^/]*)/$ /music/index.php?id=$1&sid=$2 [NC,L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ /music/index.php?id=$1&sid=$2&tid=$3 [NC,L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ /music/index.php?id=$1&sid=$2&tid=$3&fid=$4 [NC,L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ /music/index.php?id=$1&sid=$2&tid=$3&fid=$4&fiid=$5 [NC,L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ /music/index.php?id=$1&sid=$2&tid=$3&fid=$4&fiid=$5&siid=$6 [NC,L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ /music/index.php?id=$1&sid=$2&tid=$3&fid=$4&fiid=$5&siid=$6&seid=$7 [NC,L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ /music/index.php?id=$1&sid=$2&tid=$3&fid=$4&fiid=$5&siid=$6&seid=$7&eiid=$8 [NC,L,QSA]
Could the apache conf or the virtualhost break the htacces file for one line?
apache conf is the default conf (apt-get apache2)
virtualhost file
<VirtualHost *:80>
ServerAdmin wouter1994_67#hotmail.com
ServerName sites
DocumentRoot /var/www/sites
<Directory /var/www/sites>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Note: the music dir is inside the sites dir
I can't see anything wrong with the htacess file but you could just make it redirect all traffic going to music/test to music/a/b/ect
RewriteCond directives only affect the first immediate RewriteRule, so the 3 conditions you have:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !/$
Are only applied to the first rule:
RewriteRule ^([^/]*)/$ /music/index.php?id=$1 [NC,L,QSA]
And all the other rules aren't bound by those 3 conditions. The reason why the first rule probably never gets applied is because you have the condition: %{REQUEST_URI} !/$ which says if the URI does not end with a slash. But the rule itself requires that the URI ends with a slash (^([^/]*)/$). I'm going to assume that your conditions are missing a ^ in front of them and that you meant:
RewriteCond %{REQUEST_URI} !^\..+$
RewriteCond %{REQUEST_URI} !^/$
As in, does not start with a . and is not a /. You'll also want to duplicate those conditions for each of the RewriteRule entries that you have if you want them to also be applied to the other rules.

Send one subdomain to subdirectory, everything else to file with parameter

Hoo-boy I've been struggling with this one all day.
The redirect from the subdomains to the script files with subdomain passed in as a parameter work fine.
When one particular subdomain is actually a real folder (phpmyadmin), I can't seem to make it work, at least not with the previously mentioned rule in place.
Here's my stuff:
<VirtualHost *.domain.com:443>
ServerName *.domain.com
ServerAlias *.domain.com
DocumentRoot /home/domain/web
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.* [NC]
RewriteCond %{HTTP_HOST} ^(phpmyadmin)\.domain\.com
RewriteCond %{REQUEST_URI} !^phpmyadmin
RewriteRule ^(.*)$ /phpmyadmin [L]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com
RewriteRule ^\/script2$ /public/script2.php?param1=%1 [L]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com
RewriteRule ^(.*) /public/script1.php?param1=%1 [L]
<Directory /home/domain/web>
Options -Indexes IncludesNOEXEC FollowSymLinks -MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
SSLEngine on
SSLCertificateFile /home/domain/crt
SSLCertificateKeyFile /home/domain/key
</VirtualHost>
Any ideas?
2 problems with:
RewriteCond %{HTTP_HOST} !^www.* [NC]
RewriteCond %{HTTP_HOST} ^(phpmyadmin)\.domain\.com
RewriteCond %{REQUEST_URI} !^phpmyadmin
RewriteRule ^(.*)$ /phpmyadmin [L]
In your target redirect, you do not change the domain. By just using /phpmyadmin, you're not directing to www.domain.com/phpmyadmin, you're directing to phpmyadmin.domain.com/phpmyadmin, which is likely to cause an infinite redirect loop since it will keep matching the phpmyadmin subdomain each redirect. Use:
RewriteRule ^(.*)$ http://www.domain.com/phpmyadmin [L]
Unlike RewriteRule where the initial / is not used, RewriteCond %{REQUEST_URI} does need the leading slash like so:
RewriteCond %{REQUEST_URI} !^/phpmyadmin
Failing to do so means its not matching the directed /phpmyadmin, leading to another potential self-matching infinite redirect loop.
Also you don't need
RewriteCond %{HTTP_HOST} !^www. [NC]
since your url would never start with www and also start with phpmyadmin. Matching
RewriteCond %{HTTP_HOST} ^(phpmyadmin)\.domain\.com
automatically implies the NOT www match condition is also true