What does this Rewrite rule mean? - http

Im installing phpancake,
there is a folder there shema like this
application/
install/
library/
public/
sql_schema/
install.html
install.php
What does this rule mean?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /vote/public/index.php [NC,L]

The rewrite has two parts. The first one specifies that if the requested filename is a regular file with a size greater than 0 (-s), a symbolic link (-l) or a directory (-d), rewrite to nowhere, eg. take no action. [NC,L] means that the rule is non case sensitive and the last rule that these conditions match.
All other requests are forwarded to /vote/public/index.php.
The purpose of this rewrite is that an actual, existing file can be fetched from the server without interference. Without the first rule, every file request (css and js files, images etc) would go to index.php which would mess things up pretty badly.
Usually this is written in one declaration, though. You can negate the conditions, and then the [OR] statemens can be taken out also:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /vote/public/index.php [NC,L]
This is equivalent to the original statement.

Those are mainly standard rewrites which check if the requested file (or directory or symbolic link) exists on disk, in which case the file/directory/etc. should be used.
All other matches should go to /votes/public/index.php

The first rule will pass through all requests that can be mapped to a regular file with a size greater than zero (-s), a symbolic link (-l) or a directory (-d). Every other request is fetched by the second rule and rewritten to /vote/public/index.php.

Related

Rewrite .htaccess all files except

I currently have the following in my .htaccess file which rewrites all files names so I can limit access to my media.
#RewriteCond %{REQUEST_FILENAME} -s
#RewriteRule ^wp-content/uploads/(.*)$ dl-file.php?file=$1 [QSA,L]
However, I would like specific files to be ignored. For example: ^wp-content/uploads/2022/12/image-26.png and ^wp-content/uploads/2022/11/horse47.jpg
How can I update my rewrite rules to skip those specific files (and others).
I tried rewriting the original files after the above (3rd line) but does not work.
Solved thank you. Mr White
I tried rewriting the original files after the above (3rd line) but does not work.
"After" is too late, the request will have already been rewritten to your script! You would need to rewrite the files before your existing rule.
For example:
RewriteRule ^wp-content/uploads/2022/12/image-26\.png$ - [L]
RewriteRule ^wp-content/uploads/2022/11/horse47\.jpg$ - [L]
# Existing directives go here...
The L flag prevents the following rules from being processed.
Don't forget to backslash-escape the literal dot and include the end-of-string anchor on the regex.
Alternatively, add exceptions (conditions) to the existing rule. For example:
RewriteCond $1 !^2022/12/image-26\.png$
RewriteCond $1 !^2022/11/horse47\.jpg$
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^wp-content/uploads/(.*) dl-file.php?file=$1 [QSA,L]
The ! prefix negates the expression. So it is successful when it does not match.

zendframework 3 - service execute twice

I have created a personalized service to determine the users' language.
I call my service inside the Module class of my Moduel.php like this:
$languageService = $sm->get("LanguageService");
$languageService->setLanguage();
The configuration of the service is instead found inside the global.php configuration file as well as the code below.
'service_manager' => [
'factories' => [
\Application\Service\LanguageService::class => \Application\Service\Factory\LanguageServiceFactory::class
],
'aliases'=>[
'LanguageService'=>\Application\Service\LanguageService::class
]
],
The problem using xdebug the code is executed twice (constructor and methods)
I have noticed that the code is actually executed twice. The first time the url of the request is / therefore my index. The second call is the url /css/bootstrap-select.css.map. I think it's an internal call to the plugin. I do not think the behavior is correct.
The behaviour is correct. You have a problem with your server. I do not know the fix for nginx type, but for Apache, using .htaccess you should add a bit of code that serves files from the public directory straight away instead of going through the application.
By default, the Zend Skeleton Application comes with the following .htaccess file content in the public/ directory (source):
RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting or installed the project in a subdirectory,
# the base path will be prepended to allow proper resolution of
# the index.php file; it will work in non-aliased environments
# as well, providing a safe, one-size fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}/index.php [L]
The project root directory does not come with an .htaccess file. If you correctly map your server to use the project public/ directory as the projects access point, you won't need it.
Based on comment discussion, I'm assuming somewhere some config is not correct and is directing the /css/* requests to the project root. To make sure that those requests do not enter the project I have the following .htaccess in my project root:
RewriteEngine On
# If URL to the application is http://foo.com/path/to/ZendSkeletonApplication/
# the set the base to /path/to/ZendSkeletonApplication/
RewriteBase /
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Below is ZF2 default
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ public/index.php [NC,L]
Not very pretty, but it gets the job done.

Using .htaccess to redirect example.com/about.php to example.com/about/

I recently switched over my website to Wordpress. My old files were www.example.com/about.php and now its www.example.com/about/. I need to redirect incoming links from the .php extension to just the / for ALL my pages preferably using .htaccess.
What I have:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Whats in my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I guess I don't know where I'd put it in my .htaccess file either.
Thanks!
The code you have does not redirect anything. It takes a request that might be for a php file and internally appends the .php extension. Nothing happens on the browser because you've not told it to do anything. This is a 2 step process here. See the top part of this answer for a short explanation.
In order to redirect, you need to match against the incoming request, not the URI (which could have been rewritten be previous rules or iterations):
RewriteCond %{THE_REQUEST} ^(GET|HEAD|POST)\ /(.*)\.php($|\ )
RewriteCond %{REQUEST_URI} !wp-admin
RewriteRule ^ /%2/ [L,R=301]
So when someone types http://www.example.com/about.php in their browser's URL address bar, the request will look like:
GET /about.php HTTP/1.1
and the %2 backreferences about and redirects the browser to http://www.example.com/about/ (note the trailing slash) and the address bar changes.
What happens then is the browser makes ANOTHER request but this time for http://www.example.com/about/ and the server gets the URI /about/. Now you need your rule to internally rewrite it back to the php file. Unfortunately, your rule doesn't handle the trailing slash, so you need something like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]
These would all go before your wordpress rules. The wordpress rules route everything to index.php and that would wreck any URI you are trying to rewrite.
You are doing things the wrong way around. You are actually rewriting urls that end end without php to files that do end in .php.
You'd need to do something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*).php$ $1/ [NC]
I also removed the check if the file you are redirecting to exists as well as the [L] flag, because it's likely that wordpress is also doing its own rewriting, which means that you don't want this to be the last rule processed and that you won't be able to find the file on the filesystem.

Drupal newbie question

Are all requests handled in index.php?
Yes. All* requests will go through index.php there is a rewrite rule in the .htaccess file which masks this and gives user friendly urls.
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
If Drupal can't invoke these rules then you will see index.php in the browser URL.
**There are cron.php and update.php which don't but these are special files for admin so are not part of the run of the mill site.*
Yes. If you're looking for certain code snippets that handles URL parsing and calls various modules then take a look inside bootstrap.inc

Need help with .htaccess ReWrite rules

I'm using Wordpress and have the following pemalink /%category%/%postname%.
This gives me user friendly URLs like http://www.example.com/something/.
With this permalink, I will not be able to access php files directly, e.g. http://www.example.com/something/myfile.php.
I need to write a ReWrite rule which allows me access to /include/myfile.php.
Can some help me please? :)
Here's my current .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /myblogdirectory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /myblogdirectory/index.php [L]
</IfModule>
# END WordPress
Update
Ok, I got it working.
I was doing it wrong from the start. I was using the "virtual" path instead of the physical path.
Instead of /mysite/includes/myfile.php, I have to use /wp-content/themes/mytheme/include/myfile.php
I could probably also have added RewriteCond %{REQUEST_URI} !myfile.php, which would have excluded myfile.php from the rewrite rules. I have not tested this though.
Well, your rewrite rules looks good.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
means that /blog/index.php won't be serverd if %{REQUEST_FILENAME] is a physical file or directory.
More info here.
Are you sure that you're using the correct file paths?
The file you want to request should be located in /blog/include/myfile.php.
Check your error.log for apache for any related messages.

Resources