How do I use .htaccess to hide a folder and extension? - php

I want to be able to type “example.com/page” in the url and my browser to load “example.com/folder/page.php”
I literally looked at 10+ video tutorials and 20+ pages on how to do this and none of them are working properly. I can't include everything I've tried because I've tried so many with none of them working.
An explanation would also be helpful. Thanks

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^folder/(.*)$ http://www.example.com/$1 [L,R=301]

Try this
# it may also need to set mutliviews
Options +MultiViews
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]

Related

how to change php extension into aspx extension using htaccess

I'm new on php programming and i'm customizing an CRM base on PHP. I want to change the .php extension in .aspx, whether the php script is in the root file or sub directory, the .aspx should show up.
Here is my URL
I want to change THIS:
http://localhost/edev/pages/UI.php?operation=req
Into:
http://localhost/edev/pages/UI.aspx?operation=req
the code that i have tried
.HTACCESS
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://localhost/test/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://localhost/test/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
Let me know if you have any question according to this question. Thank You :)
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.aspx$ $1.php [L]

rewrite working on localhost but not on server

I'm uploading my website on my server. The rewrite rules above were working on my localhost. URL "mywebsite.com/about" served me "mywebsite.com/about.php". But something weird happens.
My htaccess file has got the right autorizations, I Know that because the other rules (deflate for exemple) are working correctly.
mod_rewrite is on, I know that because some of my rules are working. However, not everything : when typing "mywebsite.com/about" or "mywebsite.com/about.php", I get redirected to "mywebsite.com/about", but it displays a 404 error.
ErrorDocument 404 /includes/404.php
Options +FollowSymlinks
Options +MultiViews
DefaultType application/x-httpd-php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ /en [L]
### to hide '.php' extensions
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ mywebsite.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ mywebsite.com/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
###
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
I would really apreciate help on finding where I go wrong!
EDIT:
The page I'm trying to access is actually "mywebsite.com/en/about.php" by the URL "mywebsite.com/en/about"
EDIT 2:
I left nothing but this line on my .htaccess :
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [NC,L]
What it does is : strictly nothing... If I understand it well, this is the part that should redirect URL with no extensions to the .php version of it. When I go to mywebsite.com/en/about , i get a 404.
EDIT 3:
Using this other script gives the same result : rewriting the URL in "mywebsite.com/en/about" is working, but it doesn't redirect to the .php version.
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [NC,L]

extension less php site with forward slash

I have a problem for my php website, i do some research and my url look like this www.example.com/about, that's a correct output but i want to add extra forward slash so the url look like this www.example.com/about/ same with the wordpress behavior.
my .htaccess file:
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
thanks.

No access to files in subfolders when removing extension in .htaccess

I want to access some php files which are located inside some subfolders. I'm using a .htaccess file, which removes all the .php extensions.
Here is the file:
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.net/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.net/folder/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
I have to say that I just copied this .htaccess file from a tutorial, because I have no experience in this area.
So a quick example: I can access www.example.net/folder/myphpfile.php , but I get a "file not found" error when I try to access www.example.net/folder/anotherfolder/myphpfile.php .
But it works when I delete the .htaccess file.
Can someone help me with this. Thanks!
Have it this way:
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} \s/.+?\.php
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

Apache mod_rewrite is not working

I am trying to redirect requests to http://www.site.com/customer to https://www.site.com/customer and hide .php extensions, but it's not working. These are my configuration files:
httpd.conf:
LoadModule rewrite_module modules/mod_rewrite.so
mysite.conf:
<Directory /var/www/site/customer>
Order Deny,Allow
Allow from all
</Directory>
Alias /customer /var/www/site/customer
.htaccess in /var/www/site/customer:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}
#unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]
#redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.*)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.*)\.php$ $1 [R=301,L]
#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
Am I missing something?
Yep you are missing something ;)
Here are some modifications:
you forgot the redirect and last tag in HTTPS rule
you forgot the QSA directive to keep the query string when there's a redirect
So here it is, this might help, but maybe won't work 100% (but it's far still better than it was anyway):
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://$1 [QSA,NC,L]
#unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [QSA,R=301,L]
#redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.*)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.*)\.php$ $1 [QSA,R=301,L]
#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]
Two hints:
If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the .htaccess files), try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

Categories