Why does .htacces hide extension not work? - php

I would like to use .htacces for hiding the .php extension in address bar, I found the solution HERE, but this does not work for me. My .htacces in root looks like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
If I click on a link, it shows *.com/stg/xy.php
I originally would like to show just *.com, but I read, that is not a good option because of visitors, so I would be satisfied with *.com/stg/xy/
FYI: I also have another .htacceses in subfolders because of pwd protection. Should I put the same code in all of them?
I also have *.pdf files to open, I want to hide the extension, or path here too.
Thanks,
D

I think, I will use different folders with index.php and I will embed every single pdf file and use the method, mentioned above. I find this the best option in my case, because non of the "normal" methods work for me!

Related

Clean url rewriting using .htacces file in apache

I have a website where i do blogging... on my blog.php page i have posts from different categories.
when a user clicks on a link it is being redirect to /blog-details.php page with $_GET['title'] variable.
what I have tried
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#remove .php from all pages
RewriteRule ^([^\.]+)$ $1.php [NC]
#blog/title-here
RewriteRule ^blog/([A-Za-z0-9-\+]+)/?$ /blog-details.php?title=$1 [NC,L,QSA]
last line gives me something like
xyz.com/blog-details?title=hello-world
what i want to achieve is
xyz.com/blog/hello-world
not like
xyz.com/blog-details?title=hello-world
thank you, looking forward to answers.
An important thing you have to take into consideration when working with .htaccess files, is that you have to check if the path is a directory or file, then proceed to do your action.
I used your code and it worked properly, but simply added the -f and -d checks to make sure that this does not affect other pages.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#remove .php from all pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC]
#blog/title-here
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/([A-Za-z0-9-\+]+)/?$ /blog-details.php?title=$1 [NC,L,QSA]
To test out your .htaccess file and with some dummy URLs, I'd highly recommend you to check out MadeWithLove
https://htaccess.madewithlove.be/
This will let you have a test input URL and see if you get the proper output URL (the actual URL your server would read).
With that being said, I would recommend you to look into routing for future projects and self-development. A framework I'd recommend checking out is fat-free framework (f3) for PHP. It's very easy to get into, you could learn it in 2 days, and it saves a lot of time in development - by containing routing, ORM, global variables, simplified SQL queries, and plenty of other features that essentially keep you a PHP developer, rather than a framework developer.
use below method:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^blog/([0-9a-zA-Z_=-]+)$ blog-details.php?title=$1 [NC]
</IfModule>
Then you can access the title like $_GET['title'] on the page.

Rewrite PHP variables from URL with .htaccess

For example I want to change:
www.example.com/forum/thread?id=1&topic=hello
to
www.example.com/forum/thread/1/hello
I've looked around and modified my .htacess file to look like this to modify these URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^thread/([0-9]+)/([0-9a-zA-Z_-]+) thread.php?id=$1&topic=$2 [NC,L]
I keep getting a 404 error saying that the file doesn't exist. I'm wondering if it's because I'm removing the .php from the file first using this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
But then when I remove that rule and go to thread.php?id=1&topic=hello it just breaks everything and gives me masses of errors
In your question, you state that you want to change:
www.example.com/forum/thread?id=1&topic=hello to www.example.com/forum/thread/1/hello
However, that is backwards.
The address that you rewrite to needs to be the address that the server can interpret. So, www.example.com/forum/thread/1/hello should be the "friendly" address that the user enters and the rewrite should add the .php extension (although with a rewrite, this will not be visible to the user in the address bar)
Try this:
RewriteEngine on
RewriteRule ^/?thread/([0-9]+)/([0-9a-zA-Z_-]+) /thread.php?id=$1&topic=$2
The reason that you are probably getting errors going to thread.php is that you have a rule to remove the .php (which now the server will not be able to render the page). With the above rewrite rule, you will get to it by going to :
www.something.com/thread/1/hello
Your rule will never get applied because you are using RewriteCond %{REQUEST_FILENAME}\.php -f this condition checks your filesystem to see if /forum/thread/1/hello.php exists and if it doesn't then your rule is ignored. Simply comment out or remove that condition from your rule .
Try :
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^thread/([0-9]+)/([0-9a-zA-Z_-]+) thread.php?id=$1&topic=$2 [NC,L]

Removing .php extensions from sub-directory and sub-sub-directory via .htaccess

Below are the example links with current sub directories, as you can see below the htaccess code I'm using
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
currently only does the trick for - https://www.example.com/folder1/folder2/page
but not with - https://www.example.com/folder1/page.php
I'm trying to get both directories to eliminate the .php extension. Any idea how to go about this, as I have no idea how to do this. Thanks in advance
i think if %{REQUEST_FILENAME}.php is not good function.
but you can try this
RewriteCond $1 !^(index\.php|images)
RewriteRule ^(.*)$ /folder1/folder2/index.php?$1 [L]
this script will give you output like https://www.example.com/folder1/folder2/your_name_url

How rewrite TWO URLS for .HTACCESS?

One pages of site have such adds: domain.com/posts/name.php
Other pages of site have such adds: domain.com/pages/name.php
I need to cut from these adds posts/ and pages/ only.
Firstly, I tried to use in .htaccess next rules:
RewriteRule ^([A-z0-9-]+)$ /posts/$1
RewriteRule ^([A-z0-9-]+)$ /pages/$1
Don't help.
Secondly, I tried to use in .htaccess such rules and conditions:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ posts/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ pages/$1 [L]
As result: image of mistake 404
Also I tried to use such method:
RewriteCond %{REQUEST_URI} ^([A-z0-9-]+)$/(posts|pages)/([A-z0-9-]+)/$1 [NC]
RewriteRule .* domain.com/%1%3
Don't help....
what i am understanding you need to remove the post and pages from the url following code will help you for that.
You would need the rewrite module of Apache: mod_rewrite.
Then do something like this:
RewriteEngine on
RewriteRule ^post/(.*)$ $1
RewriteRule ^pages/(.*)$ $1
Here is the official documentation of mod_rewrite: click
please test it onece I dint test the code but did same for replacing url and its work for me.
Thanks.
The solution of this problem such:
We enter all information about the page in the MYSQL database:
image of database
We generate a page from the database:
generation of page (php)
Set the generated page for Human-Friendly Url:
file .htaccess
So everything works, and POSTS there are in their folder in the database, and PAGES in their folder.
folder in database
All is clean. If you can do better somewhere, then correct me, please.

Removing .php extension in localhost

I've been trying to figure out how to remove .php extension, I've searched everywhere and it seems most of the code are not working anymore. The code below is what I am using now to remove the .php extension but it is not working.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !- f
RewriteRule ^([^\.]+)$ $1.php [NC]
I'm using XAMPP on Windows 10. I also tried many times to remove the .php extension but the result was the same. But after a long while finally, I got how we can do this. If you want the same results, just follow the steps given below...
To remove the .php extension from a PHP file, e.g. if you want to change mywebsite.com/about.php to mywebsite.com/about you have to add the following code inside your .htaccess file:
First of all, make sure you've created a new file named .htaccess and placed it at your root-level/root-directory where your index file is placed.
Now, open this (.htaccess) file with any editor of your choice, copy/paste the given code and save it...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
Now, if you (any user) will access mywebsite.com/about in the browser, the user will see the content of mywebsite.com/about.php page.
But still, if you (any user) will access the URL as mywebsite.com/about.php, this will not redirect the user to this mywebsite.com/about page. But will go to this mywebsite.com/about.php page which means the user can still visit mywebsite.com/about.php page. Don't worry about it. If you want to avoid this, you can simply follow the next step.
To avoid the above problem, now you need to add some more rules in the .htaccess file. For this, you've to replace your old code with this new one given below, save your file and check it out...
RewriteEngine On
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
That's all and you're all set.
I hope this will fix everyone's problem.
Have a nice day :)
To remove the .php extension from a PHP file
for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code inside the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Or
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA]
Or
see removing .php extension from URL
Also, make sure that you've mod_rewrite on.
Also see how to create .htacess file
I think you need to change internal website coding as well with htaccess code above and remove the file name URL extension from where it is mentioned in your web coding.
Eg:-
https://yoursite.com/page.html
changed to
https://yoursite.com/page/
htaccess code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]

Categories