htaccess load resources from directory - web-services

I want to make a htaccess RewriteRule with a RewriteCond
What I have so far:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(sql.site.ro)$
RewriteRule ^(.*)$ ro-site-sql/index.php?x=$1
I want all requests sent to sql.site.ro to be served from the /ro-site-sql directory.
So if the URI is sql.site.ro/index.php the server will go to /ro-site-sql/index.php, or if URI is sql.site.ro/images/small/9954.png the server will go to /ro-site-sql/images/small/9954.png.
The problem is that if I make
RewriteCond %{HTTP_HOST} ^(sql.site.ro)$
RewriteRule ^(.*)$ ro-site-sql/$1
I get an Internal Server Error, and if I make
RewriteCond %{HTTP_HOST} ^(sql.site.ro)$
RewriteRule ^(.*)$ ro-site-sql/index.php?x=$1
I get Array ( [x] => ro-site-sql/index.php ) inside of /ro-site-sql/index.php. Should it not be just index.php, without the ro-site-sql/ part?
Thanks!

RewriteCond %{HTTP_HOST} ^sql.site.ro$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 ^(\w+/)+\w+\.\w+$
RewriteRule ^(.*)$ ro-site-sql/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sql.site.ro$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(\w+/)+\w+\.\w+$
RewriteRule ^(.*)$ ro-site-sql/index.php?x=$1 [L,QSA]
This should work to identify actual files and access those and pass anything else to index.php.

The rewrite engine loops over .htaccess files until no more substitutions occur. You need to ensure that this loop stops after the first replacement. There are various ways to do this. Perhaps the easiest to understand is:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(sql.site.ro)$
RewriteCond $1 !index.php$
RewriteRule ^(.*)$ ro-site-sql/index.php?x=$1 [L,QSA]
Add the [L] flag because this is the last rule on this cycle and [QSA] if you also have other parameters sometimes.

Related

htaccess File Rewriting API Routes

I am trying to redirect all incoming traffic to my site to index.php and put the remainder of the url in a path variable. I also have a index.html in my root directory and if the user accesses it directly I want them rerouted to the index.php script.
RewriteEngine On
RewriteBase /
RewriteCond $1 !^index\.html
RewriteCond $1 !^index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index\.html?$ / [NC,R,L]
RewriteRule ^(.*)$ index.php?path=$1 [L,QSA]
The rerouting seems to be working ok, but all my /api/index.php?route=xx&mode=xx seem to be getting rerouted too. Is there a way to exclude the api directory from the rewrite condition?
Could you please try following. This is checking if REQUEST_URI is not starting from /app/index.php then reroute everything to index.php along with passing parameters to it, couldn't test it as of now, will test it few mins or so.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/api/index\.php/?$ [NC]
RewriteRule ^(.*)$ index.php?path=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/api/index(\.html)/?$ [NC]
RewriteRule ^(.*)$ index.php [NC,L]

htaccess rewrite rule with single parameter

When user enters url like
http://example.com/app/abcd123/
I want to show hime page from
http://example.com/app/index.php?param=abcd123
Without changing URL in browser.
I put .htaccess file inside app folder with code
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index.php [NC]
RewriteCond %{QUERY_STRING} ^param=(.*)
RewriteRule (.*) http://example.com/app/%1? [R=301,L]
You can use this code in /app/.htaccess:
RewriteEngine on
RewriteBase /app/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php\?param=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?param=$1 [L,QSA]
Try this .htaccess code. It should help you.
Make sure the rewrite base should be the form the root to your present directory.
RewriteBase /app/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?param=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
In this, if the user enters like http://example.com/app/qwerty the call will be processed like http://example.com/app/index.php?params=qwerty
This should be working, I tested it. Let me know if you face any troubles.
RewriteEngine On
RewriteBase /app
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php?param=$1 [L]
Check it there: http://htaccess.mwl.be/ or other online htaccess test service

Apache Wildcard Rewriting Subdomain to a Sub Directory

Been trying to Rewrite wildcard subdomain to a subdirectory and not having allot of luck.
ie
abc.domain.com > www.domain.com/sub/abc
I have tried the folowing
// example 1
RewriteCond %{HTTP_HOST} ^(.*).domain.com [NC]
RewriteRule ^(.*)$ ./sub/$1 [L]
// example 2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(.*).domain.com [NC]
RewriteRule ^(.*)$ ./sub/$1 [L]
Hope someone can advise.
You need to use backreference from RewriteCond:
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^((?!(?:sub|index\.php)/).*)$ ./sub/%1/$1 [L,NC]

url rewriting in .htaccess

I am trying to serve different urls using mod_rewrite but whatever I try it is just not working.
An example url would be
http://www.site.com/country/tours/dynamic-part/?&city=new-york,los-angeles
And I am trying to change the url using .htaccess to:
http://www.site.com/country/tours/dynamic-part/new-york,los-angeles
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/.+city=([^\/]*)$ http://www.site.com/country/tours/$1/$2 [L,R=301]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Any ideas? I though I was close but not anymore :/
The RewriteRule does NOT match the query string, see
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#what_is_matched
So the .+city part of the rule will never match.
This should work tho...
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/ http://www.site.com/country/tours/$1/%2 [L,R=301]
The subsitution can read back-referenecs to the RewriteCond pattern.

force SSL+WWW in CakePHP .htaccess

I know the topic "How to force HTTPS + WWW" is often discussed and solved, and in general it works for me.
But as I now got a specific predefined .htaccess from CakePHP I do not know how to include it.
.htaccess for CakePHP:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
If I put the normal code for HTTPS/WWW Forcing in front or backwards to that code, it does not work properly because all requests are set to root directory and not to e.g. /contact.
Normally I am using:
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
RewriteRule ^ https://www.mydomain.com%{REQUEST_URI} [R=301]
But you can not just include that above...
Could anybody please help me including HTTPS/WWW Forcing in the above .htaccess?
The generic way is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
No need to hard-code domain name. Also request will not redirect to root when switching from http to https
Thanks LazyOne, this maybe working, but for me it often ended up in "mydomain.com/redirect:/app/webroot/index.php" which was really strange. But maybe this is due to the "{REQUEST_URI}" because I had to change my
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
to
RewriteRule ^(.*)$ index.php [QSA,L]
due to strange problems with redirect (no idea what happend, CakePHP suddenly requestet a "Redirect:Controller" as also described here http://groups.google.com/group/croogo/browse_thread/thread/55539dabfd0191fd?pli=1 - any idea about this?).
It is now working with this code:
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule (.*) https://www.mydomain.com/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.mydomain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
This is how it should be:
RewriteEngine On
# force https and www.
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
RewriteRule ^ https://www.mydomain.com%{REQUEST_URI} [R=301,L]
# route all requests for non-existing resources to CakePHP
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
another way might be to force SSL using a component ? http://bakery.cakephp.org/articles/lemon/2008/07/07/component-for-forcing-a-secure-connection