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]
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 have a simple rewrite rule that checks if a *.gz version of the requesting file exists, and if so returns that file to the browser (it's based around this technique for serving compressed CSS & JS: https://blog.jcoglan.com/2007/05/02/compress-javascript-and-css-without-touching-your-application-code/).
Assume that my CSS & JS reside in a /cache subdirectory of my site, e.g.:
http://domain.com/cache/app.css
http://domain.com/cache/app.css.gz
http://domain.com/cache/app.js
http://domain.com/cache/app.js.gz
When the following directives are placed within the .htaccess file at webroot, everything works as expected:
AddEncoding gzip .gz
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{HTTP_USER_AGENT} !Safari
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [QSA,L]
However I'd prefer to place these rules within a separate .htaccess file that sits within that /cache directory. But pasted as-is, the rules no longer work. From what I can tell, the last RewriteCond is failing, e.g. the condition which checks if the requested filename exists.
I've tried adding RewriteBase /cache/ but that didn't seem to work. I've removed my webroot .htaccess in case there was another directive there that was conflicting. I've also tried changing the RewriteRule to:
RewriteRule ^(.*)$ /cache/$1.gz [QSA,L]
And while I think that might be correct in the end, it still doesn't work - like I said because from what I can tell it's the RewriteCond which is failing.
So I'm stumped! Any help?
Following rule should work from /cache/.htaccess:
AddEncoding gzip .gz
RewriteEngine On
# Determine the RewriteBase automatically/dynamically
RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)\1$
RewriteRule ^.*$ - [E=BASE:%2]
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{HTTP_USER_AGENT} !Safari
RewriteCond %{DOCUMENT_ROOT}%{ENV:BASE}/$1\.gz -f [NC]
RewriteRule ^(.+?)/?$ $1.gz [L]
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.
I am trying to make my requests how I handle files like this
/r/login
but have that go to the server like index.php?r=login
This htaccess code that I am using does not seem to be working
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/r/([0-9]+)/$ index.php?r=$1
If anyone could help me out, that would be great!
Your code's problem is that you're using leading slash in RewriteRule. If used in .htaccess leading slash is removed by Apache. Change your rule to this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^r/([0-9]+)/?$ /index.php?r=$1 [L,QSA,NC]
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