Change URL for all php files in a folder using .htaccess - apache

Does anyone know how to change the URL for all php files inside a subfolder of the root directory and also remove .php?
http://localhost/pages/x.php
to
http://localhost/x
Example:
http://localhost/pages/example.php
to
http://localhost/example

You can do it like this, which only executes if the request exists in pages and doesn't exist in root. So existing items in root don't get rewritten.
RewriteEngine on
RewriteCond %{CONTEXT_DOCUMENT_ROOT}/$1 !-f
RewriteCond %{CONTEXT_DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^([^/]+)$ pages/$1.php

RewriteEngine On
RewriteRule ^x$ page/x.php [L]
RewriteRule ^example$ pages/example.php [L]

Related

Started implementing .htaccess but when navigating from the reduced URL, when going into a directory it then can't get back

So I have included an htaccess file to my server in the root directory and changed all of the ./ I could find set the absolutes.
However, when I search by URL into one of the directories pressing the home button does not take me home. Instead, it appends the index onto the end:
/website/book/index.php?p=home
Instead of
/website/index.php?p=home
Where have I made a fuddle?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])/$ https://%{HTTP_HOST}/paperbound/$1 [R=301,L]
RewriteRule ^([^/\.]+)?$ index.php?p=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)$ index.php?p=$1&id=$2 [NC,L]
</IfModule>
Is the htaccess used. https://sitehost/website/book/2 is URL entered and page retrieved which exists as https://sitehost/website/index.php?p=book&id=2, clicking navlink to return to https://sitehost/website/index.php?p=home, instead places https://sitehost/website/book/index.php?p=home into the URL bar and returns an error as the file does not exist.
With your shown samples/attempts, please try following htaccess rules file. Make sure to keep your index.php file is present in website folder and htaccess is present along side with website folder(not inside it).
Please make sure to clear your browser cache before testing your URLs.
##Enabling rewrite engine here.
RewriteEngine ON
##Checking conditions for non-existing pages here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
##performing internal rewrite here to index.php file.
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ $1/index.php?p=$2&id=$3 [QSA,L]

Folder is not visible in localhost

I have downloaded a web template and when i try to run that in my localhost the folder doesn't show up.that folder contains a .htaccess file.When i remove that file i can see the folder but when i open that it gives some errors.
This is the .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^$ index.php [L]
RewriteRule ^([a-zA-Z0-9]+)?$ user-profile.php?user_username=$1 [NC,L]
this is the first time i'm working with a .htaccess file...
Any help is appreciated.
Maybe problem in the index.php file PHP code? Rewrite rules looks fine.
Show us the Apache error log and a rewrite log, without that hard to say what is wrong.
It looks like the RewriteCond directive is in the wrong place...? I would have thought it should apply to the 2nd RewriteRule, not the first:
RewriteEngine On
RewriteRule ^$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)?$ user-profile.php?user_username=$1 [NC,L]
RewriteCond directives apply to the single RewriteRule that follows. That 2nd ruleset basically says that if the requested file does not exist then internally rewrite the request to the user-profile.php page passing the request (which is assumed to be a username) to the user_username parameter.

htaccess and correct url

My problem is that I can't figure out why my htaccess doesn't work.
When I check FTP path it is /var/www/html/folder/subfolder/
Since I am running WP site and I need that subfolder to be accessible, so I have this:
RewriteEngine On
RewriteBase /folder/subfolder/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php
I tried with adding html and of course removing folder but nothing is working.
Can anybody help me?
Thank you.
If you have httpd 2.2.16 or above, you should use FallbackResource instead of all those rewrite rules:
Put a .htaccess inside the folder you wish to have redirects for
Add FallbackResource /index.php
Have cake.

htaccess RewriteRule not working - (404 error) from server

I'm trying to rewrite my URL from this:
http://www.example.com/admin/index.php?id=title
to:
http://www.example.com/admin/title
I'm using this code in my htaccess:
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /admin/index.php?id=$1 [L]
But then when I try out rewritten URL's i get a 404 error from my server. What is the mistake I'm making? The .htaccess is in a subfolder called admin and the rewrite rule should only work for that folder.
This .htaccess should be placed inside the folder admin that must be inside your root folder:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /admin/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ index.php?id=$1 [L]
The 2 conditions make sure we are not redirecting an existent file or folder and rule tells we want to extract anything not a / and use as the ID.
The RewriteBase tell us your parent folder is admin and as such we work from there and onwards.

htaccess remove folder from url only

im trying to remove only a specific folder from my url. i have see many answers here but i cant find any to work for me.
i have this url http://www.mydomain.com/projects/books/index.php
i want to just remove the projects folder so i can use
http://www.mydomain.com/books/index.php
i have this .htacces inside public_html folder :
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)projects
RewriteRule ^(.*)$ projects/$1 [L]
with this code i can access http://www.mydomain.com/books/index.php but any other address redirect me inside the projcets folder
for example http://www.mydomain.com shows me the index of/ projects
thank you
You can try this rule:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(projects/|index\.php) [NC]
RewriteRule ^(.+)$ /projects/$1 [L]