Apache, rewrite/redirect all to index.php - is not empty REQUESTED_FILENAME - apache

I want to forward everything to index.php except if no file (REQUESTED_FILENAME) was specified or with other words URI is empty or take one /.
I've tried this conditions to rewrite/redirect all to index.php in the .htaccess - file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php/$1 [QSA]
If I call http://my.dom/test.php, the content of the test.php is displayed (!-f).
Next try: Check, if requested file not index.php and then redirect to index.php
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^(.*)$ /index.php/$1 [QSA,R=301]
Ok this works. But i would like no redirect by empty requested file.
Next try: Redirect if requested file is not empty and requested file is not index.php
RewriteCond %{REQUEST_FILENAME} !^$
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^(.*)$ /index.php/$1 [QSA,R=301]
If I call http://my.dom/test.php it works fine, redirect to index.php/test.php :D.
If I call http://my.dom or http://my.dom/ its also redirect to index.php.
But i would like no redirect by empty requested file.
Update (1)
I've now this in my .htaccess.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ / [QSA,R=301]
#this redirect all files to root
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php/$1 [QSA,L]
#this rewrite on server side to index.php
This doesn't work. http://my.dom/test.php was redirect to http://my.dom/. But the second rule makes an internal (550) error (every time redirect). I've put a new condition to the first section
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} !^/.*$
RewriteRule ^(.*)$ / [QSA,R=301]
#this redirect all files to root
By this: http://my.dom/test.php does not redirect.
Please tell me where my mistake is. And maybe a few solutions.
Regards

Its because / is not a file, / points to your root directory. You cant match against your root dir using Request filename variable. To exclude your homepage from the rule, just change your regex pattern to (.+) .
Or you can use the following rule
RewriteEngine on
RewriteRule ((?!index\.php).+) /index.php/$1 [L]

Related

Directory rewrite in .htaccess to another directory

I have .htaccess that does this: https://example.com/john shows the contents of the directory https://example.com/user/index.php?username=john
RewriteRule ^([a-zA-Z0-9_-]+)$ /user/index.php?username=$1 [QSA]
How can I make it so https://example.com/john/blah will show the contents of https://example.com/user/blah? Only show contents of /user folder if my previous rewrite rule was applied.
For example:
example.com/john/haha ---> example.com/user/haha
example.com/privacy/blah ---> // Will show 404 page because /privacy is an actual page.
You may try these rules in your site root .htaccess:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/user/$1.php -f
RewriteRule ^[\w-]+/([\w-]+)$ user/$1.php [L]
RewriteCond %{DOCUMENT_ROOT}/user/$1/ -d
RewriteRule ^[\w-]+/([\w-]+)/?$ user/$1/ [L]
# existing rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ user/index.php?username=$1 [QSA,L]

Deleting a directory from the path to the URL using .htaccess

There is a link like
domain.com/dir/page
It displays the contents of the page page.php, which is located in the dir directory of the site.
Current view .htaccess at the root of the site:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule .* http://%1/$0 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php/
RewriteCond %{HTTPS} =off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [QSA,L]
ErrorDocument 404 /404.php
How do I replace the URL with domain.com/dir/page to domain.com/page (that is, remove dir from the path)?
P.S. It is understood that the solution should work for all URLs of the specified type.
Try using this in your .htaccess file, make sure it is above any other rules that you have:
RewriteEngine On
RewriteRule ^dir/(.*)$ /$1 [L,R=301]
Clear your cache before you test this.

Redirect to existing file/folder if it exists in specific dir or go to index.php

I have a folder App\ressources that contains all the images, javascript library of the website.
What i'm trying to do is to redirect to the existing image/js or go to the index.php file that will handle the request and get the current module/controller/action.
So i made this :
RewriteEngine On
RewriteCond App/ressources/$1 -d [OR]
RewriteCond App/ressources/$1 -f
RewriteRule ^(.+)$ App/ressources/$1 [L]
RewriteRule ^(.*)$ index.php [QSA]
It shoud not be difficult but "debuging" is not possible.
I've asked for same question but i didn't get an answer.
Here is my workaround:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) App/ressources/$1 [DPI]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule App/ressources/(.*) $1 [DPI]
RewriteCond %{REQUEST_FILENAME} !-f # missing line, see comments
RewriteRule (.*) index.php [L,QSA,DPI]
Basically we redirect everything (what doesn't exists as file) to App/ressources and if it still doesn't exist we redirect it back to / root dir. Then we redirect it to index.php.
If file not exists redirect to App/resources/
If file still not exists redirect back to root /
If file still not exists redirect to index.php

trailing slashes in 301 redirect

I've tried to look through the multiple mod_rewrite questions, so I apologize if this is a duplicate.
I'm trying set it so that if you go to domain.com/about.php it removes .php and if you go to domain.com/about it simply remains like that.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /$1.php [L,QSA]
RewriteRule ^(.*)/$ /$1 [L,R=301]
So, right now if you go to domain.com/about it displays the page, but if you go to domain.com/about.php it doesn't remove the extension.
Additionally, I have 301 redirects
redirect 301 /our-clients http://www.domain.com/about-ourclients
That works perfect, but if the user goes to domain.com/our-clients/ with the trailing slash, they are directed to about-ourclients.php
Any advice on how to rewrite my rules?
This should do the job:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
# remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php\sHTTP/1)
RewriteRule ^(.+)\.php$ /$1 [R=301,L,QSA]
# remove trailing slash ONLY if it is not an existing folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# rewrite to FILENAME.php if such file does exist and is not a folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L,QSA]
It will redirect all direct requests to php files: /something.php will be redirected to /something
Will remove the trailing slash IF requested resource is not directory. So if you requesting /home/ and you do have such folder, then it will NOT be redirected to /home.
Will internally rewrite requests to the same named PHP file IF it does exist. If you are requesting /about and you have /about.php then it will do rewrite; If you have no /about.php then nothing happens (well, at least not on these rules -- if you have more rules then such request can be matched later .. or 404 error page will be shown).
If you are requesting /about, you have /about.php and you also have /about folder, then request will go into folder. If you do not want this to happen ( /about should always be rewritten to /about.php) then you need to remove RewriteCond %{REQUEST_FILENAME} !-d from last block. But since you have exactly the same condition in your current .htaccess then I assume it is desired behaviour.

.htaccess Rewrite Within Directory - Hide PHP extension and force trailing slash

I'm trying to hide the .php extension from my files as well as force a trailing slash on the resulting URLs.
Example: A request to /about.php would become /about/ and requests to /about would go to /about/.
The following rewrite code worked perfectly when I was in the root of my hostdomain:
RewriteEngine On
RewriteRule ^(.*)/$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://edit.mydomain.org/$1/ [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)\.php\ HTTP/ [NC]
RewriteRule .+ http://edit.mydomain.org/%1 [R=301,QSA]
However, I need to move my files into a directory of this host name. Adding a directory name to the rules and having the .htaccess in the directory itself didn't work at all and seems to cause a endless redirect.
I looked around StackOverflow and other websites and tried numerous examples and ended up with many different errors with the most common being:
Everything is an endless redirect.
Everything except the directory home page is a 500 Error.
about.php redirects to /about but there's no redirect to /about/ and /about/ displays a 500 Error.
Everything working, but the home page (of the directory) index.php when accessed without a filename goes into an endless redirect.
Things redirect to edit.mydomain.org/home/username/public_html/mydomain.org/edit/pagename.php which obviously doesn't exist.
Thanks for any help! I really need to keep these files in a directory although the .htaccess could go into the host name root if its needed.
The directory for this would be edit.mydomain.org/dave/
Save this as a .htaccess and put it in the 'dave' directory
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ http://edit.mydomain.org/dave/$1/ [R=301,L]
RewriteRule ^(.*)/$ $1.php [L]
This works for me
RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html