rewrite working on localhost but not on server - php

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]

Related

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

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]

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]

htaccess Remove trailing slash, force HTTPS, hide php extension

I've got an SSL certificate, and I need to force https on my website.
Additionally, I need to remove trailing slashes, and hide the .php extension from files.
This is what I have so far, but it's causing too many redirects:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ https://hellorufus.com/$1 [R,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ https://hellorufus.com/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# 404 Error Template
ErrorDocument 404 /404.html
I've found a number of similar questions, but when I try to piece them together for my own needs, I end up with too many redirects again!
This is a version with a few corrections that appear plausible:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1 [R=302,L,QSA]
# Redirect external .php requests to extensionless url
RewriteRule ^(.+)\.php$ /$1 [R=301,L,QSA]
# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [L,QSA]
# 404 Error Template
ErrorDocument 404 /404.html
Not sure if that already solves your issue. To be certain we would need some specific request URL that generates an endless rewrite loop.

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.

After modifying my .htaccess file to hide the .php extension from the URL, some directory items are unable to be found

Recently, I got some help modifying my .htaccess file to hide the .php extension in the url. After I got it working, a few bugs occurred with my site. For whatever reason, any php page in a sub directory returns a 404 error. For example:
mywebsite.com/test/test.php (this 404's)
but
mywebsite.com/test/test.html (this works just fine)
Since this only started occurring after my .htaccess file was modified, I am assuming the problem lies there. The file contains as follows:
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.mywebsite.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /.+?\.php [NC]
RewriteRule ^(.+?)\.php$ /$1 [R=301,L,NE]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
ErrorDocument 404 /404.php
DirectoryIndex index.php
If there is any more information I can provide to help solve this, please let me know!
Thank you for all the help.
Try these rules in your root .htaccess:
ErrorDocument 404 /404.php
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1 [R=301,L,NE]
# Redirect external .php requests to extensionless url
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /.+?\.php [NC]
RewriteRule ^(.+?)\.php$ /$1 [R=301,L,NE]
# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
If you are intended to hide your PHP project structure to be accessed by public as directory tree ... you should consider adding a .htaccess file in the root of your project with just 1 line
Add the following line to your .htaccess file.
Options -Indexes

Categories