Removal of .php for files in specific folder? - php

I have this site called precisely.me, and I have just bought a new hosting server for it.
In my .htaccess file, I want all my files within precisely.me/fpl/.php to also be accessible via precisely.me/fpl/. For eg -: precisely.me/fpl/epl-table.php should also be accessible via precisely.me/fpl/epl-table
Why is this not working?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I am using GoDaddy.com's hosting, and should admit that I have little knowledge about the .htaccess file

You may try this instead, in one .htaccess file in root directory.
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !\.php [NC]
RewriteRule ^fpl/([^/]+)/? /fpl/$1.php [L,NC]
Maps silently:
http://precisely.me/fpl/filename
To:
http://precisely.me/fpl/filename.php
Where filename is a dynamic string.
For permanent redirection, replace [L,NC] with [R=301,L,NC].

Related

How to redirect all requests within a subfolder

I have a website called mydumbwebsite.com/. In my root folder, I have various folders, one of them being "stuff". This folder is directly accessible with mydumbwebsite.com/stuff/. I want to create an .htaccess file that redirects everything that goes into this subfolder and only this subfolder, so these urls:
mydumbwebsite.com/stuff/test
mydumbwebsite.com/stuff/test/
mydumbwebsite.com/stuff/test.php
mydumbwebsite.com/stuff/test.php?test=yes
mydumbwebsite.com/stuff/test2.php (doesn't exist)
mydumbwebsite.com/stuff/test2.php?test=yes (doesn't exist)
mydumbwebsite.com/stuff/test/moar
mydumbwebsite.com/stuff/test/moar/tests.php
... should all redirect to the index.php file of the folder "stuff", even if the file/folder they point to exists.
Some additional context: despite my made up example, I encountered this problem on localhost. I have many different projects and I don't want the .htaccess of one project interfere with the other projects. I tried this, but it keeps redirecting me to the xampp homepage:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php [QSA,L]
I tried changing it to this, but that couldn't cover all instances, such as nr. 2, 3 and 4:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /stuff/index.php [QSA,L]
You need to remove the conditions !-d and !-f, which say "Don't apply the rule on existing files and directories". You also need to remove RewriteBase /.
RewriteEngine On
RewriteRule ^(.+)$ index.php [QSA,L]
The file should be located under /suff/.htaccess.
The following rules do also work for me:
RewriteEngine On
RewriteRule ^ index.php [L]
and
RewriteEngine On
RewriteRule ^ index.php [END]
Regarding your comment:
Usually I would create a public folder and put anything that should be accessed directly (public/css/, public/js, public/img etc.) in it. And whitelist it as following:
RewriteEngine On
RewriteRule ^public/ - [END] # allow direct access on public folder
RewriteRule ^ index.php [END] # anything else will be directed to index.php
You can of course whitelist multiple directories the same way:
RewriteEngine On
RewriteRule ^css/ - [END]
RewriteRule ^img/ - [END]
RewriteRule ^js/ - [END]
RewriteRule ^ index.php [END]
Or something like you suggested in the comment:
RewriteRule ^(scripts|styles|morefoldernames)($|/) - [L]
It is late, so my brain could be muddled about this, but surely you simply want everything in stuff to be redirected to index.php, no if's or buts.
Then the below should work, provided it has "/stuff/" in the URL. I'm guessing that you aren't bothered about query strings etc either, if so, then you'll need to modify the index.php below to index.php?$1 and the flags to [QSA, NC, L].
RewriteEngine On
RewriteRule ^/stuff/?$ /stuff/index.php [NC,L]
Check that your root .htaccess file has the line "RewriteOptions InheritDown" somewhere in it, that way the .htaccess file for each subfolder should be covered.
Try this out.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/stuff/(.+)$ /stuff/index.php [L]

.htaccess redirect to external url but with mod_rewrite

I have two webspaces, one domain. One webspace contains ~ 5 TB Audiofiles & Images, and one webspace is for the script.
My problem:
Take for e.g. my webspace url are domain1.tld & domain2.tld
On domain2.tld are all(!) files and and other things.
On domain1.tld i uploaded only the php files.
The problem is that if i want to see a video, which is for e.g. on domain2.tld/files/video01.mp4 and the link shows domain1.tld/files/video01.mp4 - how can i change this if this file not exist to go to domain?
I saw here a similar question.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^subdirectory/?$ http://newurl.com [L,R=301,NC]
But this have 1 problem:
If i go to http://domain1.tld/%FILE% and if this not exist -> it should be located to http://domain2.tld/%FILE%
And is it possible to work with mod_rewrite?
So i mean, if domain1.tld/%FILE% dont exist, it should be the same url but it should be the access of domain2.tld/%FILE%
Thanks!
Try that:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ http://domain2.tld/$1 [L,R=302,NC]
It means: if file or directory or symlink not found here, do redirect to domain2.tld .
(It's 302 because testing new rewrite rules with permanent redirect is dangerous )

URL rewrite through an entry.php

I have two directory under my application directory
my_app:
.htaccess
lib:
entry.php
web:
css:
css_file1
css_file2
js:
js_file1
js_file2
My url will be like this http://example.com/my_app/my_path
Under the directory my_app, there are two directories web and lib.
If the my_path can be map to any file under the web directory, use it.
For example, http://example.com/my_app/css/css_file1 should map to my_app/web/css/css_file1.
Otherwise redirect it to lib/entry.php/my_path so that the the my_path can be accessed through $_SERVER['PATH_INFO'] The redirection should be internal. The redirection should not be visible to user.
I want to write configuration in .htaccess to accomplish it.
I wrote the code as given below.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule (.*) ../web/$1
RewriteCond web/%{REQUEST_FILENAME} !-f
RewriteCond web/%{REQUEST_FILENAME} !-d
RewriteRule (.*) lib/entry.php/$1
</IfModule>
But I am getting 403 error.
I believe following should work for you in /var/www/.htaccess i.e. directly under $DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/$1/web/$2 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1/web/$2 -d
RewriteRule ^([^/]+)/(.*)$ $1/web/$1 [L]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^([^/]+)/(.*)$ $1/lib/entry.php/$2 [L]

i need some help creating Htaccess

i need to hide the extensions of my webpage and also want to let the user put the links in both (lower and upper) case:
Example:
the file name is demo.php
www.example.com/demo
www.example.com/DEMO
www.example.com/Demo
Running PHP in a LAMP server, no access to php.ini, just .htaccess
Actualy im using a file like this:
RewriteEngine On
RewriteBase /
RewriteRule ^/(OUTSOURCING|outsourcing|Outsourcing)$ outsourcing.php [NC,L]
And i m reciving this error:
Not Found
The requested URL /outsourcing was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
RewriteEngine On
RewriteBase /
www.example.com/DEMO www.example.com/Demo or doing www.example.com/DEMO/page2
RewriteRule ^/(DEMO|demo|Demo)/(.*)$ demo.php?=$1 [NC,L]
RewriteRule ^/(DEMO|demo|Demo)$ demo.php [NC,L]
RewriteRule ^/(D|d)emo$ demo.php [NC,L]
or pass anything www.example.com/DeMo www.example.com/bob to demo.php
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ demo.php [NC,L]
you may want to test if your allowed .htaccess RewriteRule /*$ http://google.com [R][L]
here is a good way to do it with case insensitive method
EDIT:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ${lc:$1}.php [NC]
this way anything entered will be redirected to a php file.
edit : this way your js and css file can still run
RewriteRule ^/[Dd][Ee][Mm][Oo]/?(.*)$ demo.php [NC,L]
will redirect any capitalization of "Demo" to demo.php
First add this line in the <VirtualHost> section OR at the end of your httpd.conf file (to enable lc function in .htaccess for later use):
RewriteMap lc int:tolower
Then have these rules in .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC]
RewriteRule ^(.+)\.php$ /$1 [NE,R=301,L,NC]
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond ${lc:%{REQUEST_FILENAME}}.php -f
RewriteRule . ${lc:%{REQUEST_URI}}.php [L]
First rule in .htaccess is doing external redirect by making a URI of /index.php to /index
Second rule in .htaccess is doing internal redirect by makign a URI of /INDEX to /index.php by lowercasing the URI. Assuming you have filename index.php physically present.
That way you can always write URLs without .php extension.

Tricky .htaccess and Drupal problem

1) I have a Drupal site located at http://example.com and I have a directory located at http://example.com/foo, but I also have a Drupal page with an alias of http://example.com/foo. How can I get the Drupal page to serve? Currently, I get a 403 forbidden page as a result of the Options -Indexes declaration in Drupal's .htaccess file, but I do not want to remove this as I do not want directories to be browsable.
EDIT: I have solved this with the following rule:
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ http://localhost/s2k/index.php?q=$1 [L,QSA]
2) To make the problem even more difficult, given the same scenario, if I have an index.html file inside the directory http://example.com/foo/index.html I always want this to take priority over Drupal's aliased page (which it does at the moment - currently I have modified the .htaccess file so that any directory with an index.html file displays it - DirectoryIndex index.php index.html).
EDIT:
So now, how can I write a RewriteCond that will look to see whether or not there is an index.html file inside the directory?
I am no RewriteRule Guru and pretty sure there is a nicer way to achieve this, e.g. with conditional RewriteCond.
But simply adding
RewriteCond %{REQUEST_URI} =/foo/
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Above the default rewrite Conds and rules make this rule kick in for /foo/ and not the Drupal-default one. Where a RewriteCond %{REQUEST_FILENAME} !-d will only apply the RewriteRule if something is NOT a dir.
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteRule ^(.*)$ http://localhost/s2k/index.php?q=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME}index.html -f
RewriteRule ^(.*)$ $1index.html [L]
RewriteCond %{REQUEST_FILENAME}/index.html -f
RewriteRule ^(.*)$ $1/index.html [L]
[normal Drupal rewrite rules]

Categories