Replicate these AliasMatches within .htaccess? - apache

I recently purchased shared hosting for an app that previously worked well on my local server via Apache Virtual Host configuration. However, my shared hosting provider (like most) does not give customers access to the httpd-conf files, meaning I have to emulate the configuration within an .htaccess file (or multiple). This presents a problem, because .htaccess does not support <VirtualHost> configuration including the very important AliasMatch directive. So my question to the community is how might one replicate the following AliasMatches using RewriteRules or Redirects:
AliasMatch /app/(.*)\.(js|css|rdf|xml|ico|txt|gif|html|png|jpg|jpeg|json|eot|woff|svg|ttf|pdf)$ "c:/wamp/www/siteroot/app/$1.$2"
AliasMatch /i18n/(.*)\.json$ "/wamp/www/siteroot/app/i18n/$1.json"
AliasMatch /(.*)\.(js|css|rdf|xml|ico|txt|gif|html|png|jpg|jpeg|json|eot|woff|svg|ttf|pdf|csv)$ "c:/wamp/www/siteroot/adminApp/$1.$2"
AliasMatch /rest/(.*) "C:/wamp/www/siteroot/rest/$1"
AliasMatch /img/(.*) "C:/wamp/www/siteroot/adminApp/img/$1"
AliasMatch /(.*) "C:/wamp/www/siteroot/adminApp/index.html"
The filepaths will obviously be changed accordingly. It seems too obvious to just do:
RewriteRule {regex} {filepath} [R=301]
Any help is greatly appreciated!

You can just remove the root part out, and use L instead of R=301 (which will externally redirect, causing the browser to request a new URL and change what's in the location bar):
RewriteEngine On
# this is to prevent looping
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteRule ^app/(.*)\.(js|css|rdf|xml|ico|txt|gif|html|png|jpg|jpeg|json|eot|woff|svg|ttf|pdf)$ /app/$1.$2 [L]
RewriteRule ^i18n/(.*)\.json$ /app/i18n/$1.json [L]
RewriteRule ^(.*)\.(js|css|rdf|xml|ico|txt|gif|html|png|jpg|jpeg|json|eot|woff|svg|ttf|pdf|csv)$ /adminApp/$1.$2 [L]
# Doesn't look like you need this rule
RewriteRule ^rest/(.*) /rest/$1 [L]
RewriteRule ^img/(.*) /adminApp/img/$1 [L]
RewriteRule ^(.*) /adminApp/index.html [L]

Related

.htaccess rule for redirecting to parent

I am trying to redirect one of my urls to the parent folder using .htaccess file. I have tried the following rule
RewriteRule ^test/(.+)$ /test/ [L,R=301]
found from htaccess wildcard redirect to parent folder but it is not working (logs show too many redirects).
I also tried the other rules below but none of them worked
RewriteBase /
RewriteCond %{REQUEST_URI} !=/test/
RewriteRule ^(.*) /test/ [END,NC]
or
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^test/(.+)$ /test/ [L,R=301]
The OS is ubuntu server. Any help or pointers is appreciated. Please let me know if I can furnish any other details to debug. Thanks
Following should work considering the parent directory is test
RewriteEngine On
RewriteRule ^(test/).* /$1 [R=301,L]
Add this to disable MultiViews:
Options -MultiViews
The Apache docs on mod_negotiation, describes what the Multiviews Option does, when enabled:
If the
server receives a request for /some/dir/foo and /some/dir/foo does not
exist, then the server reads the directory looking for all files named
foo.*, and effectively fakes up a type map which names all those
files, assigning them the same media types and content-encodings it
would have if the client had asked for one of them by name. It then
chooses the best match to the client's requirements, and returns that
document.
Use:
Options -MultiViews
RewriteEngine on
RewriteRule ^test/(.+)$ /test/ [NC,L,R=301]
In your specific folder
RewriteEngine On
RewriteRule ^rootFName(.*) /rFile.php [QSA,R=301,L]

.htaccess Rule Works in Windows but not Linux

I'm migrating a system from a Windows server to a Ubuntu server. Both are using Apache2. The rule in question is as follows:
Options +FollowSymlinks
RewriteEngine On
<IfModule mod_speling.c>
CheckSpelling On
CheckCaseOnly On
</IfModule>
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/.*_[pP]([0-9]\d*)\.cfm$ $1/index.cfm?productID=$2&%1 [L]
RewriteCond %{REQUEST_URI} ^/sitemap.xml [NC]
RewriteRule (.*) /includes/site_map/xml_site_map.cfm [L]
The goal is to rewrite a URL like this one: http://example.com/html/products/sample_product_201011_p5974.cfm into one like this: http://example.com/html/products/index.cfm?productID=5974 (the first rule above).
It works correctly on the Windows server and if I copy the exact .htaccess above into an online htaccess tester. Also, I can successfully use .htaccess to do domain redirects as well as the second rule above.
Is there an extra apache module I'm missing? I've got alias, rewrite, and speling enabled (among others that aren't relevant).

CakePHP redirect to external source under same domain

I've developed (using CakePHP) and deployed a web site on an Apache server (not managed by me) which provides a use statistics service under http://www.domain.com/stats. How do I tell the routes.php (or where should it be done) to not try to link the '/stats' with a StatsController but instead show the page provided by Apache?
Typically you put files in webroot when you want them to be accessible outside of cake. However, it sounds like this is not possible in your case. You could try adding a condition to the .htaccess file in the cake root directory.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/stats
RewriteRule ^$ app/webroot/ [L]
RewriteCond %{REQUEST_URI} !^/stats
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
I haven't tested this but the idea is to prevent redirects if the path starts with /stats which will avoid cake.
See:
http://book.cakephp.org/2.0/en/installation/advanced-installation.html
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

.htaccess help, not working when I changed servers

I am not very familiar with .htaccess and have already searched through the net but
couldn't find an explanation to what I have
RewriteEngine On
DirectoryIndex index.php
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?mpage=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?mpage=$1
RewriteRule ^search-result/([^/\.]+)$ index.php?mpage=search-result&subpage=$1 [L]
RewriteRule ^search-result/([^/\.]+)/(.*)$ index.php?mpage=search-result&subpage=$1&act=$2 [L]
RewriteRule ^online-result/([^/\.]+)$ index.php?mpage=online-result&subpage=$1 [L]
RewriteRule ^online-result/([^/\.]+)/(.*)$ index.php?mpage=online-result&subpage=$1&act=$2 [L]
This is an .htaccess to what I am building, this worked before on a GoDaddy server but
when changed server, it wont work any more.
Let say http://www.mywebsite.com/home worked before, because if you can see mpage=$1 it replaces the get to the one on slash right? But when I change servers it will see a 404 error.
Any ideas what it could be?
make sure you have the apache rewrite module enabled, and that AllowOverride is set to All in your vhost config

.htaccess to vhost

Anyone know what to put in vhost.conf for apache to replicate this (from .htaccess):
RewriteEngine on
RewriteCond $1 !^(index\.php|images|scripts|css|uploads|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Basically I want all requests except those for /images, /scripts or /css to go through /index.php.
It works when I use the .htaccess file but I'd like to know how to do it via vhost.conf as well. Anyone know if it's better to use one over the other as well (vhost.conf vs htaccess) in terms of performance, stability, etc?
It should work when prepending the pattern with /, either:
RewriteCond $1 !^(index\.php|images|scripts|css|uploads|robots\.txt)
RewriteRule ^/(.*)$ /index.php/$1 [L]
Or:
RewriteCond $1 !^/(index\.php|images|scripts|css|uploads|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
The disadvantage of .htaccess files is simply that they virtually need to be interpreted with every request while the virtual host configuration is just interpreted once when the server is started.
Gumbo++
I wrote this article on the httpd wiki to cover questions like this one.
http://wiki.apache.org/httpd/RewriteContext