I have a directory in my file structure named "finder" and I want to rewrite a url that has finder as the basename. For example take www.mysite.com/finder.
Here is the rewrite rule I'm trying:
RewriteRule ^finder/?$ /finder/directory/listings.php [NC,L]
Instead of succeeding with a rewrite i'm getting a 404 Page Not Found message. For what it's worth I have a rewrite rule that is taking this url www.mysite.com/finder/california with this rule...
RewriteRule ^finder/([a-zA-Z0-9+\-]*)/?$ /finder/directory/listings.php?s=$1 [NC,L]
.. and it's working.
I have tried DirectorySlash Off to my .htaccess but that's not working either. Any clues about what I'm missing? Thanks.
I see two potential troublesome areas:
The htaccess file with this rule cannot be in the finder folder. Make sure it is in the DOCUMENT_ROOT directory.
There could be an issue with the forward slash in the rewrite.
Tested on Apache 2.2 and 2.4:
RewriteRule ^finder/?$ directory/listings.php [NC,L]
URL in browser: http://www.example.com/finder
Redirected to: http://www.example.com/directory/listings.php
Let me know if this works.
Related
I've been using .htaccess files for 15 years so I feel reasonably confident with them. But today I discovered that the "file exists" check doesn't seem to work correctly with Apache's Alias command - and I can't figure out why not (I've read the docs multiple times and can't find anything that would explain this specifically).
e.g. this simple http conf:
Alias /site /sites/site1.com
e.g. with this simple htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /site/
# this never matches: (despite being used in most examples all over the web)
RewriteCond %{DOCUMENT_ROOT}/$1.extension -f
# this doesn't match either: RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (.*)$ FailOnPurpose [NC]
</IfModule>
...never triggers. The intent here was:
When a request comes in...
... check if there is a file with that name and the extension "extension"
... if so: rewrite
e.g. if I request:
http://example.com/site/a.png
and there's a file:
/sites/site.com/a.png.extension
then I expected the RewriteCond to work.
If I remove all aliases from apache, and use plain direct subfolders, then it works.
You cannot use DOCUMENT_ROOT inside context of alias directory because DOCUMENT_ROOT will point to a path outside alias path and your condition will always fail.
You may use this rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.extension -f
RewriteRule (.*) FailOnPurpose [L,QSA]
I have tested it on my local Apache 2.4 within an alias .htaccess
I would like to match specific file types within the root directory the website. This htaccess file also sits in the root directly.
I've got this so far:
<FilesMatch "\.(php|png|ico)$">
Allow from all
</FilesMatch>
None of my attempts to restrict the matches to the same directory have been successful. I'm sure it's something super simple.
Try to do it this way:
<FilesMatch ".*\.(php|png|ico)$">
Allow from all
</FilesMatch>
I found out that only looks at the file name. It does not look at the directory part of the filename at all.
There is a but your not able to use it in a htacces file.
So the only way is through a Rewrite Rule. Something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule "^(subdir/).*/.*\.(html|json)$" "-" [PT,L]
# -- Deny Everything Not Matched Above -- #
RewriteRule "/*" "-" [F]
</IfModlue>
The above will only allow html or json files to be accessed in a second (or greater) sub-directory within a directory called subdir in the same folder as the htaccess file. Everything else will be forbidden. Assuming mod_rewrite is installed. The files will also be treated as files, which is not the default behavior for a rewrite rule.
First question so grace is much appreciated! I have a site that use to have tens of thousands of .html files that are now index.html files inside directories. For example, file1.html has become /file1/ (/file1/index.html). I don't want to spend inodes on individual files to do this, so I was hoping it'd be possible to do in a htaccess file. I suppose it'd have to test existence of the directory which isn't actually possible right?
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/$1 -d # Dir exists?
RewriteRule ^(.*)\.html$ /$1/ [R=301,L]
Put this in your the directory that contains all directories:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/(.*)\.html$
RewriteRule .* /%1/ [R=301,L]
Here is the situation: I have got ancient PHP scripts working fine on "old" server (Apache/2.2.3, PHP as module) and I need to move it to "new" server (Apache/2.2.16 (Debian), PHP as fcgi/suhoshin)...at least my ISP tells so.
I have got very simple .htaccess file that works fine on "old" server:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/css #Just an exception for domain.com/css dir
RewriteRule ^([^/]+)/?$ %{DOCUMENT_ROOT}/index.php/$1 [NE,L]
From what I studied mod_rewrite docs and site behaviour, when
domain.com/aaa/ opened, it rewrites/opens a script as if called domain.com/index.php/aaa/
domain.com/?a=1&b=2 opened, it rewrites/opens a script as if called domain.com/index.php/?a=1&b=2
etc., optinal slash at the end of the URL.
Other "301" redirects are made in PHP according to a database results/settings, that is not important now.
Now my problem is: When I transfer this .htaccess file + PHP scripts to the "new" server, I get this error message in browser:
No input file specified.
I have been trying several hours and studying regular expressions and Apache docs, but I cannot make it working. I guess there is something different in Apache2 configuration on both servers, but cannot find what that is. Have even tried to put echo at the beginning of the index.php to make sure this error does not come from PHP, but no :-( I am sure .htaccess support is on as I can generate errors when putting wrong directive. Not a regular_expressions guru. Can anybody help please?
UPDATE: Solved by changing it to:
RewriteRule ^([^/]+)/?$ %{DOCUMENT_ROOT}index.php [NE,L]
Insteresting that /$1 is not necessary, however I had to change PHP too to $_SERVER["REQUEST_URI"] from $_SERVER["PATH_INFO"].
Can you try this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/css [NC]
RewriteRule ^([^/]+)/?$ /index.php/$1 [NE,L]
I'm trying to figure out a solution for passing all relevant HTTP requests to my index.php bootstrap file.
The two rules I want to enforce are:
Any directory,
Any file with a .html or .ajax file extension (in any directory).
None of these files or directories actually exist - bootstrap PHP file will dynamically output relevant content.
Here is my current .htaccess file
RewriteEngine on
Options +FollowSymLinks
#RewriteCond %{REQUEST_URI}$1 !-f
RewriteRule ^/?(.+(?:\.html?)?)$ index.php [L]
RewriteRule \.ajax$ index.php [L]
The RewriteCond has been commented out as it didn't seem to have any effect, and with the correct RewriteRule it will be made obsolete anyway (image files, javascript files, etc. will not be matched by the rule so don't need to be checked for existance).
I think a regex expert could easily solve this, a plus would also be to combine the two rules into one regex.
Thanks!
This rule should do it:
RewriteRule .+\.(html|ajax)$ index.php