I want to have an regular expression witch just allows to access files in a private folder. This is my code so far:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !(.*)(/public/)(.*)
#RewriteCond %{REQUEST_FILENAME} !-f #not active in this example
RewriteRule . index.php [L]
It works fine as far as I can see it's working fine but there is one thing I'm wondering about:
if there is an directory structure like /localhost/test/public and I'm calling the same url I can see the folder content, but if there isn't a public folder localhost/test/ and I'm calling /localhost/test/public it will lead to the index.php
Why isn't it displaying a 404 page?
I think UnskilledFreak is right:
If you don't put a slash (/) at the end of an url, the server "thinks" you want a file and checks if it exists. If its a directory, it'll automatic use that; if not, your rewrite of filename will match and redirect to index.php
Related
My directory setup is as follows:
Root directory at what.ever.ip (/var/www/)
User's sites are what.ever.ip/username
Users root ftp are /var/www/username/upload_files_here,
which translates to what.ever.ip/username/upload_files_here
So if user Foo uploads the file index.html he'll have to visit what.ever.ip/Foo/upload_files_here/index.html
I want to use apache .htaccess to rewrite this (without redirecting aka showing it) to display as what.ever.ip/Foo/index.html
I have tried multiple solutions without finding one that solve my problem.
So my question is how do I rewrite "what.ever.ip/username/upload_files_here/index.html" to "what.ever.ip/username/index.html" without showing it?
PS. The what.ever.ip is not known, could be an local address, localhost or an remote address. So I would prefer a possible wildcard instead of hard coding in "what.ever.ip"
Thanks, Alex.
Place this rule in /var/www/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/((?!upload_files_here/).*)$ /$1/upload_files_here/$2 [L,NC,QSA]
The following:
RewriteEngine On
RewriteRule ^(.*)/upload_files_here/(.*)$ /$1/$2 [L]
should change:
what.ever.ip/username/upload_files_here/index.html to what.ever.ip/username/index.html while showing still what.ever.ip/username/upload_files_here/index.html in the URL bar.
I'm struggling to come up with the correct code to do what I need. I've searched through SO and other sites and found answers close to what I want, but I just can't quite piece it all together right, and .htaccess is a huge weakness of mine.
I'm trying to make it so an entire folder level gets removed from all URLs on a site, otherwise preserving the structure. After that, I need to add ".html" to the end. The addition isn't anything hard, but I'm missing what I need to strip out the folder.
Starting URL: www.domain.com/ANYFOLDER/any-page-name
(Bonus: www.domain.com/ANYFOLDER/ANYDEPTH/any-page-name)
Ending URL: www.domain.com/any-page-name.html
We have a client who is moving from a static site to CMS-driven, has some great ranks/traffic for his URLs, and is petrified he will lose this (we will not take Permanent Redirects as a solution).
You can use this rule for this redirect:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*/)*((?!.+?\.html$)[^/]*)$ /$1.html [L,R=302,NC]
I was working with URLs on my webpage but I can't solve issue for URLs with 2 parameters.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-z\-]+)/?$ index.php?strona=$1 [L]
RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)/?$ index.php?strona=$1&id=$2 [L]
URLs seem fine except that when my current URL has 2 parameters (for example I'm on http://example.com/subpage/5 whole webpage is broken (stylesheets, navigation etc) because .htaccess changed all links to:
(for example navigation):
http://example.com/subpage_with_2_parameters/home
instead of
http://example.com/home
Pages with one parameter (example: http://example.com/contact) work fine.
Only solution (which is horrible) I have on mind are absolute links.
You're not the only one dealing with this problem of css, js, images paths getting messed up after implementing so-called pretty URLs. I am seeing these problems being reported on SO almost every day.
You can solve this problem in 3 ways:
Best solution is to use absolute paths for images, css and js files i.e. start your path with / or http://
Another option is to use base href tag in HTML head section like this:
<base href="http://www.example.com/">
3rd option is via mod_rewrite
Put these lines above your other RewriteLine in your .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteRule ^[^/]+/([^.]+\.(?:js|css|jpe?g|png|gif))$ /$1 [L,R=301,NC]
It's not your rewrite rules which are breaking your stylesheets, but your actual HTML. The same thing would happen if you had an actual directory called foo and placed index.php in there.
If you write Home, that link is relative to the current directory (in URL-space) so on a page with a URL like http://example.com/foo/bar/baz it links to http://example.com/foo/bar/home.
What you want instead is for the link to be relative to the root of your domain; for that, you need a leading slash: Home
The only reason this seemed to work before is that all your URLs were in the root directory, so "current directory" and "root of domain" were the same thing.
I need some help on the htaccess subdomain to be pointed to specific file in a folder.
Inside the parent folder should contain individual php files to correctly path the right applications. Sounds simple here.
The url that the user has type should not change / redirect, instead it should only
"link" to the correct file at the background
Example when user type userbase.company.com it should point to
http://company.com/parent/userbase.php
Similarly, when user type userbase2.company.com it should point to
http://company.com/parent/userbase2.php
but no redirection should happen. url links should remain at
http://userbase2.company.com
folder would be the same, but not the file. Is it possible?
Currently I have this htaccess code:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^userbase.company.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.userbase.company.com$
RewriteRule ^(.*)$ \./parent/userbase.php/$1 [L]
I'm going have to put this at /public_folder/ right? Not at the /public_folder/userbase ?
This rule should do the job:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/parent/
RewriteCond %{HTTP_HOST} !=domain.com
RewriteCond %{HTTP_HOST} !=www.domain.com
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$
RewriteRule (.*) /parent/%1.php%{REQUEST_URI} [L]
URL will remain unchanged in browser (means rewrite -- internal redirect).
It will only work for something.domain.com (not domain.com or www.domain.com).
It will rewrite this request http://user1.company.com/hello/pink/kitten.php into /parent/user1.php/hello/pink/kitten.php
If subdomain is complex (e.g. www.something.domain.com) then it will be reflected in rewrite as well: e.g. http://www.something.company.com/hello/pink/kitten.php will be rewritten to /parent/www.something.php/hello/pink/kitten.php. If such behaviour is undesired (I do not know your requirements for 100%) then you will need add 1 more condition to the rule.
Request for a home page (e.g. http://user1.company.com/) will be rewritten in this way: /parent/user1.php/
P.S.
Rule was tested before posting -- works fine, but you have to check and maybe tweak it if it needs to be working with your CodeIgniter app (sorry, I'm not familiar with that framework).
In any case this rule should be above your CodeIgniter rewrite rules (if there are such).
Hi I have a problem.
I want to get all requests to redirect to index file in main directory and I've achieved this but there are problems with relative paths.
When I put address like: mydomain.com/something it works ok as the paths are relative to the main directory.
The problem is when I put something like: mydomain.com/something/somethingelse.
the .htaccess file:
Options FollowSymLinks
RewriteEngine On
# ignore anything that's an actual file
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
# redirect all other traffic to the index page
RewriteRule . index.php [L]
Any ideas on how to get css/js working?
Edit:
The problem is that css/js files aren't loaded when the path entered have multiple slashes like:mydomain.com/something/somethingelse
It is no doubt better to use absolute path for static files (css, js, images etc). But if you lots of those instances in several pages then consider using HTML base tag to specify a default URL for relative paths. eg:
<base href="http://www.example.com/static/" />
Using the <base>-tag is a nice solution and most browsers seem to handle it well. Except there are some issues with IE, as was to be expected... Apparently you can also run into some other funny problems, see discussion here.
So for people where this is not an option, i have looked into the alternative (the "hard way").
Usually you store css/js/static images/other stuff like this:
index.php
js/
css/
imgs/
and you want the javascript and stylesheets etc. to be available, no matter how many slashes there are in the url. If your url is /site/action/user/new then your browser will request
/site/action/user/css/style.css
/site/action/user/css/framework/fonts/icons.ttf
/site/action/user/js/page.js
/site/action/user/js/jquery/jquery.min.js
/site/action/user/js/some/library/with/deep/dir/structure/file.map
So here are some rewrite rules for apache to solve this... First, if the target actually exists on disk, do not rewrite:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [L,QSA]
In words, IF reqest filename is a directory OR IF request filename is a file then do not rewrite (-), last rule (L) and pass any GET parameters (QSA, query string append). You can also use
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^.*$ - [L,QSA]
if you also need symlinks. Next we want the javascript and stylesheets to be found even if the requests assume a wrong base directory as shown above.
RewriteRule ^.*/js/(.*)$ js/$1 [L]
RewriteRule ^.*/css/(.*)$ css/$1 [L]
The pattern is pretty obvious, just replace 'css' with the directory name. There is still a problem with this, especially for large websites with lots of javascript and stylesheets, libraries etc. - The regex is greedy. For example, if you have a javascript directory like this:
js/some/library/js/script.js
and your request goes to /site/action/user/new, the browser will request /site/action/user/new/js/some/library/js/script.js, which the rewrite-engine will then rewrite to
js/script.js
because the first .* is greedy and matches /site/action/user/new/js/some/library. Switching to non-greedy regex does not really make sense, since "the rewrite engine repeats all the rules until the URI is the same before and after an iteration through the rules."
There is another problem, and that is that for every directory that needs to be exempted from rewriting, a relatively "expensive" regex is needed. Both problems can be fixed by just putting every static component into a subdirectory with an "unusual" name (and really this is the best solution imo - anyone with a better idea please post it).
The directory structure would then look like this:
index.php
mystrangedir/js/
mystrangedir/css/
mystrangedir/imgs/
Of course, this needs to be inserted everywhere in the code - for projects with a large existing codebase this can be tricky. However, you only need a single regex for directory exemption then:
RewriteRule ^.*/mystrangedir/(.*)$ mystrangedir/$1 [L]
Automated build systems (like gulp, grunt....) can be used to check if "mystrangedir" does not exist as directory anywhere below itself (which would again throw off the rewrite engine).
Feel free to rename mystrangedir to something more sensible like static_content but the more sensible it gets, the more probable it is that the directory name is already used in some library. If you want an absolutely safe directory name that has certainly never been used before, use a cryptographic hash, e.g. 010f8cea4cd34f820a9a01cb3446cb93637a54d840a4f3c106d1d41b030c7bcb. This is pretty long to match; you can make a tradeoff between uniqueness and regex performance by shorting it.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
Should obviously work despite the comments.
Try to add the RewriteLog and RewriteLogLevel directive to give us better details.
This is a path resolution issue: When using the relative path ./css on the base path /something it is resolved to /css while on /something/somethingelse it is resolved to /something/css.
This can’t (or rather shouldn’t) be fixed with mod_rewrite. Use absolute paths instead of relative paths, so /css instead of ./css.