Treat an incomplete filename in a URL as PHP? - php

I didn't write this web code. I just have to deploy it....
The $WEBROOT directory includes index.php and login.php, among others.
The automatic redirect URL from index.php (which successfully executes when just the domain URL is requested) is http://$HOST.$DOMAIN/login?p=$VALUE
This returns the code 404.
If I manually change the URL to http://$HOST.$DOMAIN/login.php?p=$VALUE
the login page successfully appears.
My first problem is I don't know what keywords to search for in the Apache documentation for this. This question seems close. But, my actual files have the .php extension. My problem is that I have to assume all of the URLs will just request file and not file.php.
How do I tell Apache to look for file.php before it returns a 404 for not finding file?

Create a file named .htaccess with the following content inside the folder your .php files are in:
#turn on url rewriting
RewriteEngine on
#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Related

How can I maintain a website where a folder and file have the same name but also hide the extension of that file?

I am trying to improve the structure of my urls by using .htaccess. I have a file named foo.php and a folder named /foo. I want to be able to access example.com/foo and show example.com/foo.php. I also want to be able to access example.com/foo/bar and show example.com/foo/bar.php. Lastly I want to reditrect from example.com/foo/ to example.com/foo. Does anyone know how to do this?
This is my code:
RewriteEngine On
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
I am able to remove the .php extension by I am not able to redirect from the folder to the file. I also tried the solutions below but none of them resulted in the desired behaviour.
htaccess - Rewrite files that have a directory with the same name
.htaccess, rewriting of filename with same name as directory
.htaccess, proper rewriting of directory and file with same name
I found a workaround solution to this problem. In order to remove the extension from file names, I used the following code in my .htaccess file:
RewriteEngine On
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The above code allows me to go to example.com/foo and show example.com/foo.php. In order to redirect from example.com/foo/ to example.com/foo, I created an index.php file in the /foo directory. I added the following PHP code to the file
<?php
$page = str_replace('/index.php', '', $_SERVER['PHP_SELF']);
header("Location: $page");
?>
This redirects from the directory to the file. Hope this helps.

I am redesigning a PHP website in HTML. Do I need to set up redirects because of the file extension?

I am working on redesigning a website that is currently a PHP website. The new site will be all HTML. I am trying to keep all of the slugs the same. Should I set up redirects?
Currently, the website pages have the .PHP extension in the browser.
Example: https://www.dehartsystems.com/residential.php
The new page will be HTML and the URL will have no file extension.
Example: https://www.dehartsystems.com/residential
Do I need to set up redirects as the file extension will be changing?
To answer your question, yes you do need to set up redirects. Typically this is done in the .htaccess file in the root of your html folder on your server.
In this .htaccess file you put:
# Check rewriting is possible.
<IfModule mod_rewrite.c>
# Turn on Rewrite engine
RewriteEngine on
# check if file does not exist.
RewriteCond %{REQUEST_FILENAME} !-f
# check if folder does not exist.
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite the given URL.
RewriteRule ^(.*).php$ $1.html [R=301,L]
</IfModule>
This will redirect any call to any .php file that does not exist to the HTML file with the same file name, in the same folder location.
Reference:
https://htaccessbook.com/add-remove-change-file-extensions-htaccess/
See also How to change the PHP file extension using .htaccess file on GoDaddy Linux Hosting?
Yes, you will need to rename every file to .HTML if you don't do so the browser won't recognize the file as code. " https://www.dehartsystems.com/residential.html " only then your website will work., i.e you should rename residential.php to residential.html

Url rewriting and redirrecting not working (quite)

I have a WordPress installation in my main folder, so my site goes like
www.example.com
Besides that I have a subfolder with a separate static site that you can access with
www.example.com/special
So in the special pages I have a page called
www.example.com/special/my-special-page
It's actually a .php file, but I've removed the extensions in the .htaccess file of that special site with this
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
And this works.
But my client said: I want, when I enter
www.example.com/myawardspage
to go to
www.example.com/special/my-special-page
but to have the above URL (without /special/my-special-page) in it. So in the main .htaccess file (the one that controls the WordPress) I've added
RewriteRule ^myawardspage?$ http://www.example.com/special/my-special-page [NC,L]
So now when you go to
www.example.com/myawardspage
I am redirected to
www.example.com/special/my-special-page
which is great, but I need the URL to look like
www.example.com/myawardspage
So in the .htaccess file of the special page I've added
RewriteRule ^myawardspage/?$ /special/my-special-page [NC]
But the URL remains the same (http://www.example.com/special/my-special-page).
What am I doing wrong here?
Just below RewriteEngine on, place the following lines in your /.htaccess (document root) file:
# Check if the file being requested exists
# in the 'special' directory
RewriteCond %{DOCUMENT_ROOT}/special/$1.php -f
# If so, then rewrite accordingly:
RewriteRule ^([^.]+) special/$1.php [L]
Using this method means that you don't need that rule in the special/.htaccess file - you can safely remove it.
Ok, so you mean when ever you want to hit www.example.com/myawardspage then you want to get data from www.example.com/special/my-special-page while in your address bar your address remains www.example.com/myawardspage To achieve this apply this rule in your WordPress installation's .htaccess file.
RewriteRule ^myawardspage/?$ www.example.com/special/my-special-page [NC,L] # Handle requests for "www.example.com/special/my-special-page"
It wil get you desired data and your problem will be solved. Let me know if that works for you.

.htaccess default directory behaviour

I have just started learning about .htaccess files for Apache, I have a website set up so that all requests should come through my index file (which is called Main.php).
"Webpages" are then acquired through a wp GET var (such as wp=forum) - i wish to make this instead Domain/directory/Forum instead of the current Doman/directory/Main.php?wp=Forum
The problem i am facing is that all my "webpages" are stored in their own directory and are made up of "webparts" so forum will be a sub directory of "forum" with files inside it that make up the page. This is causing problems with my redirecting.
I have created the following .htacess file:
#turn redirect engine on
RewriteEngine On
#make sure URI is not a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^?]*)$ Main.php?wp=$1 [NC,L,QSA]
This works fine on the redirecting until it is a directory that is entered as the "wp" at which time it will add a trailing slash and corrupt the wp GET var passed (forum/ would be passed not forum as needed)
Here are 2 examples of how I think it may be working:
Main (not a file or directory)
loop 1: Hits the rewrite changes to Main.php?wp=Main
loop 2: URI is file (no change)
MainContent (is a directory)
loop 1: Hits the default directory change? (guess) Changes to
MainContent/ (note the trailing slash)
loop 2: Starts .htaccess and changes this to Main.php?wp=MainContent/
loop 3: URI is file (no change)
Moreover, whenever a file is accessed in my websever with a trailing slash after to (so example: Main.php/) it will display with no links or included files. It will just show the file being requested and seems to ignore any and all css ... php includes or anything else (is this default Apache settings?).
Sorry for the long and possible confusing post. If I need to clean anything up just shout.
Try with that:
#turn redirect engine on
RewriteEngine On
DirectorySlash Off
#make sure URI is not a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ Main.php?wp=$1 [NC,L,QSA]
It might be helpful to look at the .htaccess file for Drupal, which does the same thing - i.e. redirect all requests through the index file. The most relevant snippet from that file is:
# Pass all requests not referring directly to files in the filesystem to index.php?page=[rest of url]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
The second condition above "! -d" might solve the problem you describe but you might still need to do some "cleaning" in your php code. It's worth looking at the whole Drupal .htaccess file as it does other things to tighten security, such as blocking access to certain files etc.

Rewrite everything after a certain directory to that directories index.php (htaccess)

Hi, so this may be asked elsewhere but I have searched and come up with irrelevant results.
I clearly don't know what to search for exactly.
I'm just trying to rewrite everything after a certain directory to that directories index.php.
Here is an example of the URL a visitor would SEE
website.com/search/location/United%20States
And I would like that URL to be rewritten server-side so that it loads website.com/search/location/index.php
(not a 301 redirect)
I would like the Url to stay the same but load the index.php script (to include United%20States so this can be passed to PHP to determine what the location is and if it is legitimate etc.).
Sorry I know that this will be somewhere already but I just can't find it
I have some code already but it is buggy and seems to choose when it wants to work and also sometimes uses location/index.php/United%20States which is not what I want.
Put this code in your htaccess (which has to be in your root folder)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/location/.+$ /search/location/index.php [L]
If you have Apache web server, make sure you have mod-rewrite enabled and put .htaccess file into your WEBROOT/search/location directory. Put this into .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [L]
This will internally redirect all requests, where file or directory does not exists, to index.php.
You could also put .htaccess file into your WEBROOT directory and write this into to:
RewriteRule ^search/location/.* /search/location/index.php
Hope this helps.

Categories