modrewrite how can all files to index.php? - .htaccess

actual .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule index.php [L]
target:
All files should point to index.php
also png/xml ect.
call on browser:
http://localhost:8080/test.png
browser message:
enter image description here
log:
[Sun Feb 6 01:23:29 2022] [::1]:57052 Accepted
[Sun Feb 6 01:23:29 2022] [::1]:57052 [404]: GET /test.png - No such file or directory
[Sun Feb 6 01:23:29 2022] [::1]:57052 Closing
my server start to test with:
php -S localhost:8080 -dxdebug.mode=debug -dxdebug.client_host=127.0.0.1 -dxdebug.client_port=9003 -dxdebug.start_with_request=yes
my actual server:
PHP 8.1.1 (cli) (built: Dec 17 2021 22:38:05) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.1, Copyright (c) Zend Technologies
with Xdebug v3.1.2, Copyright (c) 2002-2021, by Derick Rethans
with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies
works on macbook ...
have an idea to solve my problem?

PHP 8.1.1 (cli) (built: Dec 17 2021 22:38:05) (NTS)
You are using the built-in PHP webserver. .htaccess (and by extension mod_rewrite) is an Apache config file and is not supported by the PHP webserver.
To implement a front-controller/router using the PHP built-in webserver you need to use PHP, as described in the PHP docs:
https://www.php.net/manual/en/features.commandline.webserver.php
Aside: The mod_rewrite rule is also incorrect and won't work as written:
RewriteRule index.php [L]
You are missing the first argument to the RewriteRule directive, so it will never match the request you are making. It should be something like this:
RewriteRule . index.php [L]
Where . (a single dot) is a regular expression (pattern) that matches at least 1 character (ie. everything except the document root, which is handled by the DirectoryIndex).
Further Reference:
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
works on MacBook ...
You must be using Apache, with a different .htaccess file!

Related

How to increase Request line/fieldsize limit?

I want to encode a long Base64 encoded string within the URL.
Currently long URLs (over 300 chars) that are rewritten under mod-rewrite result in a forbidden(402) error.
If my request URL is 355 characters it results in error, however when I reduce it to 300 it works.
Is there a default value in the server that limits the request line size?
I couldn't find the LimitRequestLine/FieldSize directive anywhere in my server configuration.
Here is my rewrite rule:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^json/ base64json.php [L]
A URL looks something like this:
http://sub.mydomain.com/json/longrandomnumbers?param=xxx
I wish to extract randomnumbers and param from the rewritten url, but it says forbidden permission denied, inside apache2 error log, it says:
Mon Mar 19 21:03:31.711559 2018] [core:error] [pid 20504] (36)File name too long: [client 167.220.24.128:45071] AH00036: access to /json/eyJldmVudHMiOlt7InNlc3Npb25JZCI6InMxNTAxMjY0OTIzeDYyZjg4MTNlNjkzMWM5eDU4MjQ0MjY2IiwiaW5zdGFudGlhdGlvbiI6IjkxMTMwMDQ3MTQ3OTE1ODgiLCJpbmRleCI6MCwiY2xpZW50VGltZXN0YW1wIjoxNTIwMzg3NTkwLjE2OSwibmFtZSI6ImNyZWF0aXZlTG9hZGVkIiwidmlld2FiaWxpdHkwME1lYXN1cmFibGUiOnRydWUsInZpZXdhYmlsaXR5NTAxTWVhc3VyYWJsZSI6ZmFsc2V9XX0= failed (filesystem path '/var/www/track/json/eyJldmVudHMiOlt7InNlc3Npb25JZCI6InMxNTAxMjY0OTIzeDYyZjg4MTNlNjkzMWM5eDU4MjQ0MjY2IiwiaW5zdGFudGlhdGlvbiI6IjkxMTMwMDQ3MTQ3OTE1ODgiLCJpbmRleCI6MCwiY2xpZW50VGltZXN0YW1wIjoxNTIwMzg3NTkwLjE2OSwibmFtZSI6ImNyZWF0aXZlTG9hZGVkIiwidmlld2FiaWxpdHkwME1lYXN1cmFibGUiOnRydWUsInZpZXdhYmlsaXR5NTAxTWVhc3VyYWJsZSI6ZmFsc2V9XX0=')
and
failed (filesystem path
'/var/www/track/json/eyJldmVudHMiOlt7InNlc3Npb25JZCI6InMxNTAxMjY0OTIzeDYyZjg4MTNlNjkzMWM5eDU4MjQ0MjY2IiwiaW5zdGFudGlhdGlvbiI6IjkxMTMwMDQ3MTQ3OTE1ODgiLCJpbmRleCI6MCwiY2xpZW50VGltZXN0YW1wIjoxNTIwMzg3NTkwLjE2OSwibmFtZSI6ImNyZWF0aXZlTG9hZGVkIiwidmlld2FiaWxpdHkwME1lYXN1cmFibGUiOnRydWUsInZpZXdhYmlsaXR5NTAxTWVhc3VyYWJsZSI6ZmFsc2V9XX0=')
I tried to add RequestLine/FieldSize to apache2.conf, no use, wondering how to by pass the limit for the file system I use and why it's so annoying to check the length somehow before redirect to the correct path which is base64json.php, could I do it as a long parameter instead? It might be caused by ubuntu files system.
I am using Apache2.4.7 on Ubuntu 14.02.
This must be defined before loading any Virtual Host.
Add this in yours apache2.conf, right.
Did you restarted apache daemon after edit?
sudo service apache2 restart

Problems with .htaccess after upgrading to PHP 5.4

I have had some difficulties with my site after updating my servers to use PHP version 5.4.. I have been through a lot of support tickets and browsed the internet for 2 days.. Now I need to ask directly to the people who actually know about it.
After updating the php version I started to get a 500 error on my pages and it turned out that my rewrite rules in my htaccess file wasn't working anylonger. They did in php version 5.2 but not in 5.4.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
# If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+website\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteRule ^([\w.-]+)/?$ website.php?id=$1 [L,QSA]
RewriteRule ^([\w.-]+)/([\w.-]+)/?$ website.php?id=$1&page=$2 [L,QSA]
</IfModule>
Can anybody tell me what I have to do differently? I would really appreciate it.
EDIT:
I got this copy from my livechat with hostgator:
[Thu Jun 26 03:54:07 2014] [error] [client ] malformed header from script. Bad header=<br />: website.php
[Thu Jun 26 03:55:35 2014] [error] [client ] Cannot load the ionCube PHP Loader - it was built with configuration 2.2.0, whereas running engine is API220100525,NTS
[Thu Jun 26 03:55:35 2014] [error] [client ] The Zend Engine API version 220100525 which is installed, is newer.
[Thu Jun 26 03:55:35 2014] [error] [client ] Contact Zend Technologies at http://www.zend.com/ for a later version of Zend Optimizer.
[Thu Jun 26 03:55:35 2014] [error] [client ]
Try adding this line:
zend_extension = /usr/local/ioncube/ioncube_loader_lin_5.4.so
to your php.ini
SOLUTION IS NOW FOUND
I made the wrong assumption to think it was due to my rewrite rule. The problem seemed to be at HostGator. If you host at HostGator, this will be helpful for you. The reason all this started was because HostGator has announced, that the will upgrade the servers to run PHP 5.4 as standard instead of PHP 5.2. What they didn't mention, was the structure of their servers and how individual sites has to manage their individual PHP files.
Well I found out that by changing the name of my php.ini file in the root directory (making it invalid for the server to use), the system located a PHP version specific php.ini file in the main servers directory. This made me realize that the php.ini file in my root directory had to be changed completely, so therefor I created a new php.ini file containing the 5.4 version. The site didn't change, because the files were basically identical, but it made it possible for me to use the EZConfig in cPanel again.
This is what to do:
Change PHP version either in the PHP configuration tab in cPanel, or
by adding AddHandler application/x-httpd-php54 .php to your
.htaccess file.
Open File Manager in cPanel.
Locate your root folder (Home) and find the php.ini file.
If you want to keep your existing php.ini file as a backup, rename it to ex. php_old.ini.
Create a new file named php.ini and insert the content from following link: https://wiki.cac.washington.edu/display/webpub/php.ini+for+PHP+5.4
Good luck with it. Hope this will solve it for somebody as well, so that they don't have to spend 3 days solving it themselves.

Redirect works fine but I get File does not exist in logs

I have this .htaccess file
DirectoryIndex index.php
RewriteEngine on
RewriteRule ^user/([_.0-9a-zA-Z]+)$ show-user.php?user=$1 [L]
which redirects just fine in the browser, but if on the error log on my server I can see hundreds of "File does not exist" errors, do you have any idea why?
Here is an error example from the log:
[Thu Mar 28 02:58:43 2013] [error] [client xxx.xxx.xxx.xxx] File does not exist: /home/folder/public_html/user, referer: domain/user/username0001
And there are hundreds! But everything works fine! What can I do to make these errors not show up?
Fixed, I had to use /show-user.php like this
RewriteRule ^user/([_.0-9a-zA-Z]+)$ /show-user.php?user=$1 [L]

mod_rewrite rules logging "Config variable ${REQUEST_URI} is not defined" on every request

I have the following .htaccess on an Apache/2.4.2-win32 server:
# Turn mod_rewrite on
RewriteEngine On
# Allow direct loading of files in the static directory
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^/?static/(.+)$ - [L]
# Send all other requests to controller
RewriteCond ${REQUEST_URI} !^/?(spf/index\.php)?$
RewriteRule .* spf/index.php [L,QSA]
This works well and does exactly what I want it to. For those of you who can't be bothered working out what it does, it sends all requests through spf/index.php unless they are for a file that exists in the static directory.
The file resides in the virtual host's documentroot.
Every request that falls through this .htaccess generates the following error:
[Wed Aug 01 14:14:16.549835 2012] [core:warn] [pid 7100:tid 1076] AH00111: Config variable ${REQUEST_URI} is not defined
This is not actually causing a problem - every request works as expected - but it's filling up my error log and I don't like it.
According to Google, no-one has ever had this error before. That's as far as I've got with debugging it, I don't really know where to go next.
Anyone got any idea what's going on here?
P.S. I'm aware this might be a question better suited to SF, if the general opinion is that it doesn't belong here I'll move it.
You need to replace the $ with a %:
RewriteCond ${REQUEST_URI} !^/?(spf/index\.php)?$
to
RewriteCond %{REQUEST_URI} !^/?(spf/index\.php)?$

Charset problem in .htaccess while using Chinese, Japanese, Turkish, Russian

My .htaccess works perfect on live server but shows 500 error when setup on local machine. If I remove Chinese, Japanese, Turkish, Russian rules from it it works perfect but it necessary to have those charters in it.
Example:
RewriteRule ^会員専用サイト\/今すぐ登録.php /JP/members/join_now.php?rw=$0&lang=JP&%{QUERY_STRING} [L]
RewriteRule ^Члены\/Регистрация.php /RU/members/join_now.php?rw=$0&lang=RU&%{QUERY_STRING} [L]
RewriteRule ^üye\/Kayıt-ol.php /TR/members/join_now.php?rw=$0&lang=TR&%{QUERY_STRING} [L]
RewriteRule ^멤버구역\/가입.php /KR/members/join_now.php?rw=$0&lang=KR&%{QUERY_STRING} [L]
also used chartset to UTF-8 in htaccess but no success
Local machine using WAMP
OS: Windows XP, Windows 7
I've tested it successfully on a Debian 6.0 machine + windows7 wamp. So it should be working.
Of course you need to make sure your files are well encoded and apache can handle them.
For wamp: open the httpd.conf file from the notification icon. Notepad should pop-up.
Save the file as : select UTF-8 and filter *.*
Please make sure that the file is saved as httpd.conf and not httpd.conf.txt
Add the rules.
Restart the services. Should work.
FYI
Tested on Debian 6.0:
RewriteRule ^会員専$ /stack/redirect.php [L,R]
Is working fine for me. Here is the log:
strip per-dir prefix: /var/www/会員専 -> 会員専
applying pattern '^会員専$' to uri '会員専'
rewrite '会員専' -> '/stack/redirect.php'
explicitly forcing redirect with http://mysite.org/stack/redirect.php
escaping http://mysite.org/stack/redirect.php for redirect
redirect to http://mysite.org/stack/redirect.php [REDIRECT/302]
strip per-dir prefix: /var/www/stack/redirect.php -> stack/redirect.php
applying pattern '^会員専$' to uri 'stack/redirect.php'
pass through /var/www/stack/redirect.php

Resources