mod_rewrite to not visible file - .htaccess

I'm missing something about .htaccess.
working tree like:
/
/subdomain
/subdomain/.htaccess
/subdomain/index.php
/assets
/assets/...
the htaccess is inside subdomain and I'm accessing this like: http://subdomain.localhost/
assets is outside of subdomain and I did this rule:
RewriteRule ^assets/(.*)$ http://localhost/assets/$1
Now, when subdomain/index.php tries to load something like assets/style.css the rewriterule redirect to http://localhost/assets/style.css.
The problem appears when I try to load a javascript file. It's recognized as a different domain, and doesn't work properly.
Is there a way to make http://subdomain.localhost/assets works as (and not a redirection) http://localhost/assets via some magic rewriterule or something else?
I tried ^assets/(.*)$ ../assets/$1 but it seems not working.

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]

.htaccess rewrite rules not working

I am using scssphp for my css preprocessing and right now my styles look like src="style.php/style.scss". What I was wanting is to use a htaccess to just write in the style and then anything ending in .scss would get run through style.php. So I tried putting this in my home directory.
RewriteEngine On
RewriteRule ^(.*)\.scss$ style.php/$1.scss
I even tried
#RewriteEngine On
#RewriteRule ^(.*)\.scss$ style.php/style.scss
Neither work, something is happening, because my style.scss is loading with a 500 internal server error, but I'm not sure where the error is.
Your rules are looping, try adding an additional check to not rewrite when the URI has style.php in it:
RewriteEngine On
RewriteCond %{REQUEST_URI} !style\.php
RewriteRule ^(.*)\.scss$ style.php/$1.scss
The rewrite engine will continue to loop through your rules until the URI stops chanmging. What's happening with your rule is that a request like /path/style.scss is getting procfessed and rewritten to /style.php/path/style.scss, and the the rewrite engine loops. The second time around, the rule gets applied again and it rewrites to: /style.php/style.php/path/style.scss, etc.
a better regex to get requested file name would be [a-z_A-Z]+.scss
try your htaccess using this syntax

Custom URL rewriting not working in codeigniter

I use the code below in a Codeigniter htaccess file
RewriteRule ^([^.]+)-vacation$ /search?location=$1
When i access it with the url www.example.com/moscow-vacation,
it gives me a 404 error.
I have used the above htaccess code in a core php application. It works fine there.
Most likely, you don't have a search script. If the script is named search.php, you can use this slightly modified rule
RewriteRule ^([^.]+)-vacation$ /search.php?location=$1
If you have a controller named Search with a method location, you can either rewrite directly to
RewriteRule ^([^.]+)-vacation$ index.php/search/location/$1
or try
RewriteRule ^([^.]+)-vacation$ search/location/$1

Use htaccess to mask folder name

Here's a problem I'm always wanting to solve with htaccess. I haven't found a way yet, though it looks like it should be possible - perhaps someone can help.
Let's say I have a folder at the root of my site called /foo/. I want users to be able to access that folder at the path /bar/, but for various reasons I can't rename the folder.
So as not to create confusion I only want one path to ever be seen - that is to say, I don't want people to use the name /foo/ to access the folder; they should always use /bar/. If someone goes to example.com/foo/, their browser should redirect to example.com/bar/ - but the content returned should be the content of /foo/.
To make matters more complicated, pages in /foo/ have dependencies (images, stylesheets, links to other pages, etc) within /foo/ which are hardcoded and can't be changed. These must, of course, still work.
So, to summarise, this is what I want :
Requests for example.com/foo/ should redirect to example.com/bar/.
Requests for example.com/bar/ should return the contents of example.com/foo/.
Is this possible? It looks on the surface as if it would create an infinite redirect... but I'm pretty sure there are ways to prevent that in htaccess, aren't there?
I'd be very grateful for any help.
(PS - for a little extra background: The normal reason I want to do this is to rename the wordpress /wp-admin/ directory to something more professional and easy for customers to remember, such as /admin/. But the same system should work for masking any path in this way.)
I found a sort of workaround - by using a symlink and htaccess in combination.
First I created a symlink from /bar to /foo/.
Then I put this in htaccess :
Options +FollowSymLinks
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
This has exactly the desired result - example.com/bar/ shows the content of the /foo/ directory, and example.com/foo/ redirects to example.com/bar/
But if anyone can come up with a pure htaccess solution I'd much prefer that!
Update :
Ok, I've finally found out how to do this. It turns out to be quite simple...
RewriteCond %{THE_REQUEST} ^GET\ /foo/
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
RewriteRule ^bar/(.*)$ foo/$1
The only problem is that it doesn't take account of RewriteBase, so you have to include the full path in the first line (after ^GET\).
If I understand correctly what you want is something like this inside your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^foo/$ bar/
RewriteRule ^bar/$ foo/
</IfModule>

Resources