.htaccess with public folder - .htaccess

I have a directory structure with the following on localhost:
http://localhost/testing/
A directory structure exists inside of testing as follows:
/testing/public
/testing/public/index.php
/testing/public/img
/testing/public/css
..etc for the js and swf directories
A .htaccess file is inside the testing folder and the contents are as follows:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /testing/
RewriteRule ^public$ public/ [R,QSA]
RewriteRule ^public/$ public/index.php?state=public [L,QSA]
RewriteRule ^stackoverflow$ stackoverflow/ [R,QSA]
RewriteRule ^stackoverflow/$ public/index.php?state=stackoverflow[L,QSA]
I am using PHP and inside of the /testing/public/index.php file I wanted to test that the $_GET['state'] is indeed saving the variable.
When I try to test out:
http://localhost/testing/public
$_GET['state'] is not found at all BUT
http://localhost/testing/stackoverflow
does indeed echo out that $_GET['state'] equals 'stackoverflow'.
What am I missing here??? Why is it that I cannot get the state=public in the first link? Thanks for the help!

This works fine on my system, but I think you are getting into trouble by having a rewrite rule with the same name as an actual file system directory. The file-system will generally take precedence. So when you load up '/testing/public' it just loads /testing/public/index.php without running your rule at all.
Try changing the rule to this:
RewriteRule ^public_$ public_/ [R,QSA]
RewriteRule ^public_/$ public/index.php?state=public [L,QSA]
Navigate to 'testing/public_', if that prints out 'public' as expected then you will know that was your problem.

Related

redirect single image from one directory to another

I have one website, say www.example.com. So when I access "http://www.example.com/program/resources/foo.gif" it serves me foo.gif image from /usr/share/roundcube/program/resources/foo.gif, I found that in access log.
So what I would like to do is, I would like to copy that image from /usr/share/roundcube/program/resources/ to my webroot /var/www/www.example.com/webroot/img/ and write rewrite rule so that when request comes for foo.gif, it should serve from /var/www/www.example.com/webroot/img not from /usr/share/roundcube/program/resources/.
I've tried this
RewriteEngine On
RewriteRule /program/resources/(.*) /img/$1 [L]
It's working fine. But what if I want to make a rule for single image that is foo.gif ?
You can make it more generic and simpler than that.
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/override%{REQUEST_URI} -f
RewriteRule .* override/$0 [L,NS]
Now just put any file you don't want to come from RoundCube into the override folder, e.g. override/program/resources/foo.gif

Rewrite rules for localhost AND live environment

I want to add rewriterules that works in local environment (localhost) and on my liveserver.
Why?
I don't want to change rules when I test my project locally and upload it to the liveserver.
Adding Rules
Here an example
(ANY-URL)index.php?page=somepage
change to
(ANY-URL)/somepage
So I used a rewritemod generator and pasted this into it:
index.php?page=somepage
The rewriterule I got, looks like this: (of course my .htacces starts with RewriteEngine On)
RewriteRule ^([^/]*)$ /?page=$1 [L]
When I try to get to (http:)
//localhost/myproject/development/index.php?page=login it sends me to the root directory of my local development envirment. But the URL in the adressline doesn't change.
Testing
Of course I tried some other Rules by pasting the whole URL into the generator just to test if the rewrite thing works.
Also here the URL doesn't change to short-url but the server cant find stylesheets and javascripts anymore. I got redirected to the index.php
Possible solutions?
Maybe it has something todo with that "RewriteBase"?
Do i have to set a basepath?
My .htacces is located here:
//localhost/myproject/development/.htaccess
Later I also want to change paths that look like this:
(ANY-URL)index.php?page=somepage&second=hello&third=world&fourth=cool
Also here a I'm looking for a solution that works on both environments.
Since the htaccess is located inside a sub-directory, use RewriteBase:
RewriteEngine On
RewriteBase /myproject/development/
Once that is done, you use the base element in your php/html files. This will resolve the addresses for the scripts/stylesheets in your pages.
<base href="/myproject/development/" />
Now, the rewrites in htaccess would be:
RewriteRule ^((?!index\.php)[^/]+)/?$ index.php?page=$1 [L]

How to write htaccess rewrite for subdirectories recursively?

I have an Apache server, hosting a number of websites for my company. I plan to use htaccess, and rewrite the URLs from the "root" directory to a subfolder.
Here is my real folder structure:
/www (root)
/www/beamstyle (beamstyle.com.hk, CodeIgniter framework)
/www/beamcard (beamcard.com.hk, Static files)
/www/beamcard/app (beamcard.com.hk/app, CodeIgniter framework)
====================================================================
The beamstyle website works using the following code:
ReWriteCond %{HTTP_HOST} ^(www.)?beamstyle.com.hk [NC]
RewriteCond %{REQUEST_URI} !^/beamstyle/.*$
RewriteRule ^(.*)$ /beamstyle/$1 [L]
The above works, because my framework (CodeIgniter) is inside /www/beamstyle.
Therefore, I can access http://beamstyle.com.hk, and it will get redirected without any issues.
====================================================================
However, here is the problem. When I do the beamcard website, it doesn't work because actually, the directory "/www/beamcard/" actually stores ONLY static .html files. My CodeIgniter framework is inside app/.
When I use the same code:
ReWriteCond %{HTTP_HOST} ^(www.)?beamcard.com.hk [NC]
RewriteCond %{REQUEST_URI} !^/beamcard/.*$
RewriteRule ^(.*)$ /beamcard/$1 [L]
Here are the results using the above code:
(a) http://beamcard.com.hk/ <-- OK no problem, because this is the immediate folder after rewrite (and contain static files only)
(b) http://beamcard.com.hk/app <-- NOT OK, because it steps one directory beyond the immediate folder after rewrite.
If I type this, the location bar (on the top) gets un-disguised and redirected to http://beamcard.com.hk/beamcard/app/ (I confirmed that this redirect not done by Codeigniter files because the same result happens when I apply on an empty directory)
====================================================================
I've tried everything I could, and did so many Google searches, but failed to fine an htaccess snippet that works on subdirectories BEYOND the directory after rewrite. It loses it's "disguise" functionality when I step further in the subdirectories.
Any help on this is greatly appreciated!
Cheers,
Thomas
====================================================================
Update 1
In order to describe my situation better, I've put together some use cases.
[ Case Scenarios ]
** To simplify things, let's assume the "app" directory is empty **
(1) If I type in http://beamcard.com.hk, the page "/www/beamcard/index.html" inside the server should get loaded. After finish loading the page, the location bar should write "http://beamcard.com.hk/".
(2) If I type in http://beamcard.com.hk/contact_us.html, the page "/www/beamcard/contact_us.html" inside the server should get loaded. After finish loading the page, the location bar should write "http://beamcard.com.hk/contact_us.html".
(3) If I type in http://beamcard.com.hk/app, an "empty file listing" should be loaded. After finish loading the page, the location bar should write "http://beamcard.com.hk/app/".
(4) If I type in http://beamcard.com.hk/app/, an "empty file listing" should be loaded. After finish loading the page, the location bar should write "http://beamcard.com.hk/app/".
========================================================================
Currently, (1) and (2) works.
However, for (3) and (4), after finish loading the page, the location bar got redirected to "http://beamcard.com.hk/beamcard/app/", which "reveals" the "/beamcard/" portion, which ideally should be hidden from the site visitor.
Try adding NS at the end (to prevent the rule from being applied to sub-requests):
ReWriteCond %{HTTP_HOST} ^(www.)?beamcard.com.hk [NC]
RewriteCond %{REQUEST_URI} !^/beamcard/.*$
RewriteRule ^(.*)$ /beamcard/$1 [L,NS]

Show index page for all links to folder

I have a folder named "keywords" for my website, such as this: http://www.mysite.com/keywords/
In the folder I only have an index.php file. Regardless of what someone types in after the folder, I need the index.php to show. For example, http://www.mysite.com/keywords/this-is-a-test should show the index.php file's content but without changing the URL.
Everything I've tried seems to fail - I'm working with the .htaccess in my /keywords/ folder. Regardless of what I put there I seem to keep getting a 404 error. Any help would be appreciated, I'm sure it's something small and simple that I just don't know how to do.
This is what I currently have:
RewriteEngine on
RewriteRule - index.php [L]
The problem with this is that it only work when going to links with a dash in them.
For example,
http://www.mysite.com/keywords/testing-this
does work. But this does not work:
http://www.mysite.com/keywords/testingthis
Any help would be appreciated.
RewriteEngine On
RewriteBase / #make sure your website is located in root directory, if not you have to add directory here...
RewriteRule ^keywords/testingthis$ /keywords/index.php$1 [L]

Rewrite rules for subfolders

This may seem like a silly question but I can't figure it out.
let's say I have a public_html folder with various folders like: Albatross, Blackbirds, Crows and Faqs.
I want to make it so that any traffic to Albatross/faqs.php, Blackbirds/faqs.php, Crows/faqs.php etc will see the file that is at faqs/faqs.php?bird=albatross or faqs/faqs.php?bird=crows or what have you.
If I go into the Albatross folder's .htaccess file I can do this
RewriteRule faqs.php$ /faqs/faqs.php?cat=albatross[QSA]
Which works fine, but I want to put something in the top level .htacces that works for all of them, so tried:
RewriteRule faqs.php$ /faqs/faqs.php?cat=albatross[QSA]
RewriteRule /(.*)/faqs.php$ /faqs/faqs.php?cat=$1 [QSA]
and even
RewriteRule /albatross/faqs.php$ /faqs/faqs.php?cat=albatross [QSA]
and various others but nothing seems to work, when I go to http://www.birdsandwhatnot.com/albatross/faqs.php I see the same file the same way it's always been. Does the presence of an .htaccess file in the subfolder conflict with the higher up .htaccess file?
Am I missing something?
A small correction should do the trick
RewriteEngine on
RewriteRule ^(.*)/faqs.php$ /faqs/faqs.php?cat=$1 [QSA]
"/" is not being passed to parser.
Hope it helps

Resources