How to get file directory trough .htaccess by using `RewriteRule ^(.*)$ ?id=$1 [L,QSA]`? - apache

How to get file directory trough .htaccess by using RewriteRule ^(.*)$ ?id=$1 [L,QSA]?
If .htaccess is located in http://localhost/some/dir/.htaccess and I'm opening http://localhost/some/dir/here/I/use/RewriteRule/, how I detect value /some/dir/ without using RewriteBase and without manual adding %{DOCUMENT_ROOT}/some/dir/, like value localhost I get trough %{HTTP_HOST}?

If you do not use RewriteBase you need to tell mod-rewrite the real Directory Root /var/ww/mysite/some/dir in the rewrite rule. RewriteBase would take the location url and map it to the directory.
So you'll maybe end up with
RewriteRule /var/ww/mysite/some/dir/(.*)$ ?id=$1 [L,QSA]
And trying to map some internal variables it may be
RewriteRule %{DOCUMENT_ROOT}/some/dir/(.*)$ ?id=$1 [L,QSA]
But I'm unsure, I rarely use mod_rewrite in .htaccess -- I prefer Directory tags, and the file path management can be different in .htaccess (auto removal and adding of directory prefixes). If you do not find a solution try to ask Servfault, plenty of admins other there.

Actualy Apache still does not have pathinfo($,PATHINFO_DIRNAME), function like has PHP.
So on now there are solution on using %{REQUEST_URI}, like this example:
RewriteRule ^(.+)/$ /path-dirname/$1 [R=301,L]
may reset with:
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^.+/$ %1 [R=301,L]

Related

Apache re-write URL path that doesn't exist

We currently have a website with a URL structure as follows:
https://www.example.com/en_CA/homepage/page1.html
https://www.example.com/en_CA/homepage/page2.html
We need to shorten the URL to:
https://www.example.com/page1.html
https://www.example.com/page2.html
We have tried using the following rewrite rules and conditions:
RewriteCond %{REQUEST_URI} !^/en_CA/homepage/ [NC]
RewriteRule ^/(.*)$ /en_CA/homepage/$1 [P]
The problem we have is that we get a 404 because the shorter URL doesn't exist. I think the solution needs to also involve AliasMatch to set up an alias for that URL but I'm not sure how to go about that.
I've tried:
AliasMatch ^/[^/]*/(.*) /en_CA/homepage/$1
RewriteCond %{REQUEST_URI} !^/en_CA/homepage/ [NC]
RewriteRule ^/(.*)$ /en_CA/homepage/$1 [PT]
But this doesn't work.
The website is build using Adobe AEM so we need to ensure that AEM only ever receives the long URL.
Thanks
Russell
There is no need to use AliasMatch, I think, you want to access the url https://www.example.com/en_CA/homepage/page1.html from https://www.example.com/page1.html, and the same to the other one. Please let me know if I am wrong.
Try this, let me know if it works:
Please read the comments (text after # symbol) carefully
# Add this to your root .htaccess file i.e the public_html, htdocs, etc. or use RewriteBase /
RewriteEngine On # Do not use this two times in one .htaccess file, be sure you don't have any other directories other than /en_CA/homepage/ in your root dir, or use the RewriteRule ^(.*)$ /dirname/$1 [L] for every dir.
RewriteCond %{REQUEST_URI} !^/en_CA/homepage/
RewriteRule ^(.*)$ /en_CA/homepage/$1 [L]
I am sure the above will work.
Follow the same to other folders.
I will add something later to hide the folder containing it.

htaccess adding WWW and changing filename in subdirectory

I know similar questions have come up, though often without a working answer. I'm hoping to have better luck!
I have an .htaccess file in my root directory adding "www" to everything:
RewriteEngine on
RewriteCond %{SERVER_NAME} ^mysite.org
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=permanent,L]
This generally works fine. I have a subfolder (/myquiz/) in which the old index.html file has been replaced with index.php. I know there are external links to /myquiz/index.html, so I want to make sure those redirect. Leaving index.html in place and trying to redirect from that led to some odd behavior, but adding an .htaccess in that directory works for that:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html?$ /myquiz/index\.php [NC,R]
Trying to load index.html redirects to index.php as hoped for, and the WWW gets added if needed. But requesting mysite.org/myquiz/index.php directly does not add the WWW.
I tried adding "RewriteEngine inherit", but that resulted in calls getting redirected to my root folder instead. A great trick if I want to make a subfolder inaccessible, but not helping here. I also tried just adding the code from my root .htaccess into the beginning of my subfolder's .htaccess, but that worked no better.
Any ideas?
You shouldn't need to add another htaccess file in the myquiz folder. This should work in the htaccess file in the root of the site. Remove the htaccess file in myquiz and try this.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.org
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=301,L]
RewriteRule ^myquiz/index\.html$ /myquiz/index.php [R=301,L]
Also I wouldn't use %{SERVER_NAME} unless your are sure the name is set properly in the config file. Then it can be more reliable than HTTP_HOST, otherwise I would instead use %{HTTP_HOST}.
I think inherit would work if you add an L flag to the rule that you have in your myquiz folder:
RewriteRule ^index\.html?$ /myquiz/index\.php [NC,R,L]
So that it redirects first, then the inherited rule (the www) gets applied after.
You could also just put both rules in the same file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.org$ [NC]
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=permanent,L]
RewriteRule ^myquiz/index\.html$ http://www.mysite.org/myquiz/index.php [R=permanent,L]

RewriteRule webroot to file-system path

Have checked out lots of topics here and there regarding the issue in question,
but no results yet.
So here is the issue.
Two projects:
proj: site.loc
proj2.0: newsite.loc
They are located in different places on the file system.
And i need to redirect from one project to another internally,
so url for site.loc is preserved.
E.g. requesting site.loc/hey/there i need apache to serve files form proj2.0.
First, i know that on the .htaccess level we cannot use RewriteRule to file-system path
(for security reasons).
Okay, an Alias is a workaround.
Say i add an Alias to virtual host as following:
Alias /newsite /some/path/to/proj2.0
Then if i'll add the rule to proj's .htaccess:
RewriteRule ^hey/there /newsite
This will work.
But, the webroot does not work:
RewriteRule ^$ /newsite
Is it i'm doing something wrong or there is some quirk about the webroot ?
Thanks in advance
You should not rewrite to your root, but suppress it. Here is how I have done it in my project to suppress the www
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>
There should be a way to make it work with your /newsite in the same manner.

preventing direct download of SQLite file using .htaccess

I know the basics of .htaccess, but freely admit to being a non-expert when it comes to mod_rewrite.
The problem:
I have a sqlite db file in the document root (no, I can't move it out of here) called data.sqlite. This file is accessed internally by PHP files, however, I want to protect this file from being 'downloaded' by the user typing the db URL directly into their browser.
I have used .htaccess to create pretty URLs in the past, and thought using mod_rewrite would provide a nice solution to my problem. However, my rewrite rule does not seem to prevent access.
.htaccess
Options +FollowSymLinks
Options -multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{http_host} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,NC]
##prevent access to any file data.sqlite by redirecing back to index.php
RewriteRule ^data.sqlite$ index.php [L,NC]
##my other rules follow - not shown here
Any ideas where I'm going wrong with the rewrite?? I'm sure it is something simple?
EDIT:
Ideally, I'd like to prevent direct URL access to all files ending in .sqlite, not just data.sqlite
Here's how you do it because I have also done it:
RewriteCond %{HTTP_REFERER} !^http://*.webwarecollection.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://*.webwarecollection.com$ [NC]
RewriteRule .*\.(sqlite)$ - [F,NC]
About "start with ." that is not true!. File must start on ".ht" to be blocked by default on appache.
httpd disables access to files that start with a . by default. Rename your file to .data.sqlite and it will be dealt with.

Use symfony 1.4 without changing apache configuration

Is it possible to set the /web directory as webroot without changing apache configuration file?
I tried using the following .htaccess code, but if i go to localhost/module/, it displays 404 error. But if i go to localhost/web/module/ then everything works.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule sf/(.*) lib/vendor/symfony/data/web/sf/$1 [L]
RewriteRule ^$ web/ [L]
RewriteRule (.*) web/$1 [L]
</IfModule>
i do like this on the root :
RewriteEngine On
RewriteBase /
RewriteRule (.*) ./web/$1 [L]
And edit web/.htaccess uncommented the 'RewriteBase /' line.
this make all the mysite.com/aaaa/bbbb works like mysite.com/web/aaaa/bbbb
Short answer: no.
Bit longer: you will have to edit the apache config at least to give it permission to access the web/ directory, so even if you symlink your web folder to /var/www, it will not work.
This is quiet similar to my question Symfony on virtual host (document root problem).
This is my .htaccess in the project root directory:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/images/ [OR]
RewriteCond %{REQUEST_URI} ^/js/ [OR]
RewriteCond %{REQUEST_URI} ^/css/
RewriteRule ^(.*)$ /web/$1 [L]
RewriteCond %{REQUEST_URI} !^/web/
RewriteRule ^(.*)$ /web/index.php [QSA,L]
This solved my problem but symfony tries to generate every url (eg. using url_for) from the document root so instead of url like domain.com/my-article it generates domain.com/web/my-article.
I had to slightly modify my PatternRouting class to trim the /web prefix from each url. I think this is not the best solution but it works.
Also if I want to access backend application I have to call always /web/backend.php/ because I don't want to have so many rewrite rules in the .htaccess.
If you want to see my extended PatternRouting class source code I'll paste it here.
Yes, it is possible. Copy everything from web/ up a level to your document root. Edit index.php to reflect the fact that everything it includes is now one level closer to its current directory than it used to be (one less ../). You won't have to edit a single other Symfony file.