I have a php website. The various pages are distributed in various directories. Now, when I try to send the link to any page to a user, I have to put some URLs like "www.mydomain.com/collection/latest/requestpage.html" or "www.mydomain.com/api/get/someapi.php". But this URL is too complicated. I want the pages to be referred via the URLs like "www.mydomain.com/collection/requestpage" and "www.mydomain.com/someapi" and also this same address to be displayed in the url bar of browser.
Thus, loading "www.mydomain.com/collection/requestpage" takes the user to "www.mydomain.com/collection/latest/requestpage.html" and loading "www.mydomain.com/someapi" takes the user to "www.mydomain.com/api/get/someapi.php".
So,
1) I want the actual directory structure to be hidden from the URL,
2) I want to hide the file type extension of the page being loaded, and
3) Show this modified URL in the address bar of the browser when accessing the specified page.
I know this can be done using a .htaccess file but I have never written anything like this before. I have a .htaccess file on my directory that redirects any request coming to "mydomain.com" to "www.mydomain.com". I want to add some code to this htaccess file to do the tasks I mentioned. Please suggest what requires to be done.
Thanks in advance.
1) to remove the directories from the URL just add the following codes to your .htaccess file
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^FOLDER_NAME/ FOLDER_NAME%{REQUEST_URI} [L,NC]
Change FOLDER_NAME with your actual folder names
2) to remove extensions from the URL use following code
RewriteEngine on
RewriteBase /
for html
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
for php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
EDIT: while working with POST requests you have to change the RewriteRule as following
RewriteRule ^ %1 [R=307,L]
3) You are done with what you wanted
Hope this helps.
Related
I have found some roque URLs within Google Search Console which I am trying to remove. Somehow Google has found a number of URLs with a .php file extension.
Within Stackoverflow I found this similar question : URL Extensions removal
I added the following lines of code to my .htaccess :
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(.+)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I was hoping that if a browser requests a url with the file extension intact, it would re-direct to one without.
I don't think it is the above code that is failing, more likely a setting on my server - what other reasons would make this code not do as intended ?
https://www.martincorby.co.uk/test.php
https://www.martincorby.co.uk/test
I made a simple admin interface for one website and then I wanted to hide the .php from URL. But then the login stopped to work.
Login form (post method) is on page admin.php in root directory, after login it includes admin content from another file in protected directory.
What am I doing wrong?
.htaccess looks like this:
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.php\ HTTP/
RewriteRule ^(.*)\.php$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
EDIT: I noticed not only login form, but also all the ajax loaded modal dialogs aren´t sending data. Where I could, I removed redirections in forms and in ajax file I put url:file instead url:file.php where it was possible. And these corrections helped. But on some lines I need to have the extension written. So I can´t use this and still need some proper settings in .htaccess for these few cases.
You have made your .htaccess very complicated.
if only url-rewriting is your goal then you can do it simply by
*Replace YOUR_DIRECTORY by your folder name or remove it with slash.
RewriteEngine on
RewriteRule ^([a-zA-Z0-9-_]+)/?$ YOUR_DIRECTORY/$1.php [NC]
Note :- There is no need to forbid requests with .php extension, because you have
sad that these files exists in another protected directory, then it
will automatically throw 404 error.
I am using .htaccess file for url rewriting . I've written all pages with .php extention and to hide them i am using this code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]`
But this hides when i write "www.example.com/abc.php" to "www.example.com/abc" . I want them to change automatically like when i click on a link
example
then page link should open like "www.example.com/example" not "www.example.com/example.php"
Also how to hide "?var="hello" from all pages but variable should be sent to that page is that possible ?.
UPDATE
For example here i am using this
<?php
if(isset($_GET['hi'])){
echo $_GET['hi'];
}
?>
a<br>
b<br>
c<br>
d<br>
I want what ever the page is should be accessed without extension and query string should be hided but must be accessed.
Use this .htaccess:
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
For hidding ?val you should use POST instead of GET. And take a look on this article for .htaccess he has defined good in here.
Regarding hiding the query string. When the php page runs it can first check for a query string. If one is found you can pull the data out of the query string, store it somewhere (session probably) and redirect to the page with no query string (and no file extension for that matter).
However if you do this you're obscuring most of the benefits of using a query string.
To remove the php extension completly, you can use :
RewriteEngine on
#1redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ %1 [NC,L,R]
#if the request is not for a dir
RewriteCond %{REQUEST_FILENAME} !-d
#and the request is an existing php file
RewriteCond %{REQUEST_FILENAME} !-f
#then rewrite the request to orignal php file
RewriteRule ^([^/]+)/?$ $1.php [NC,L]
I have a question about removing the ending of a file in the url bar (the .something like .php, .html, .js, .asp part). My goal is such that whenever somebody is typing in the url something like www.123.com/a where the file is, let's say a php file( so it's a.php ) so that people can type in www.123.com/a instead of www.123.com/a.php. How do I fix this?
Is there some unique way to save files so that they are saved and can be called via the url just by its name? In my case, if I call www.123.com/a it will say "object not found error" but it will work for www.123.com/a.php.
How do I hide the .something part of a file in the url bar?
Additional notes, I use notepad so whenever I save I type in a.php as save as for PHP or a.js for javascript files.
Typically this is done with .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [QSA]
Use .htaccess!
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
https://stackoverflow.com/a/10469791/1533203
http://eisabainyo.net/weblog/2007/08/19/removing-file-extension-via-htaccess/
How do i fix this? Is there some unique way to save files so that they
are saved and can be called via the url just by its name?
No.
What actually happens is we get the web server to re-write the requests before they get to PHP.
So the user might type (or be sent to):
http://stackoverflow.com/questions/14026969/remove-url-file-ending-php-js-asp-etc
Apache (or whatever web server you are using) will then re-write that to something like:
http://stackoverflow.com/index.php?page=questions&id=14026969&title=remove-url-file-ending-php-js-asp-etc
From which we can process it.
create .htaccess file if is bot exist on repo folder and add that code to it
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
I currently have a .htaccess file that allows people to enter the URL without the php extension, such that http://domain.com/account redirects to account.php
I would like to be able to have it so that if I enter http://domain.com/account/contactinfo (or http://domain.com/account/settings/groups and so on) it still goes to account.php, but I am not sure how to change what I have to achieve this.
Current .htaccess :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(([A-Za-z0-9\-_\.]+/)*[A-Za-z0-9\-_\.]+)?$ $1.php
Any help appreciated! Obviously if there exists a folder it should follow that path (e.g. if /folder/page.php exists, then http://domain.com/folder/page/create would go to folder/page.php)
Try this is you don't need to pass any URI info into query string (i.e. your app will still look at $_SERVER['REQUEST_URI'])
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9\-_\.]+)(/[A-Za-z0-9\-_\.]*)?$ $1.php&q=$2 [QSA]
# Note the optional '&q=$2' on line above if you want to make removed part of URI available as passed parameter
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ / [L,QSA]
Note that since I removed the condition to check for a valid php file, I added a second conditional rewrite rule to just redirect to site root if the re-written request does not point to a valid PHP file. You could obviously redirect this to a 404 page or whatever else you might want to redirect to. Or you could remove this altogether and let Apache give it's default 404 response.