Remove php extension from subfolder url - php

I am trying to add some other page for my project. But i have one question about php extension.
So i am using htaccess for some url like following htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
RewriteRule ^friends/([\w-]+)/?$ /friends.php?username=$1 [L,QSA]
RewriteRule ^followers/([\w-]+)/?$ /followers.php?username=$1 [L,QSA]
RewriteRule ^status/([\w-]+)/?$ /status.php?msgID=$1 [L,QSA]
RewriteRule ^following/([\w-]+)/?$ /following.php?username=$1 [L,QSA]
RewriteRule ^followers/([\w-]+)/?$ /followers.php?username=$1 [L,QSA]
RewriteRule ^(.*/([a-zA-Z0-9_-]+)|([a-zA-Z0-9_-]+))$ /profile.php?username=$1 [L,QSA]
Now, i have created photos.php from the details folder and the link is :
Go Photos
When i click the Go Photos link i want to remove that url php extension like
http://www.name.com/details/photos
I have still tryed some answer from stackOverflow
Remove .php extension from url
Remove php extension from url
but that answers gives me 404 page not found.
How can i remove .php extension from url anyone can help me here ?

You need additional rules for removing .php extension.
Use these rules:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^status/([\w-]+)/?$ status.php?msgID=$1 [L,QSA]
RewriteRule ^(following|followers|friends)/([\w-]+)/?$ $1.php?username=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule (?:^|/)([\w-]+)/?$ profile.php?username=$1 [L,QSA]

Related

facebook style user profile URL using htaccess

Hi everyone i need help about htaccess. I am trying to make a facebook style profile url like : https://www.example.com/azzo
So the user_profile.php is in the pages folder. Now user profile link is this http://www.example.com/pages/user_profile?username=azzo So i want to cut the pages file name from the url. What should i do to use my url like facebook (https://www.example.com/azzo) .
I am using the following htaccess
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^group/([\w-]+)/?$ pages/group.php?group_username=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^(.+?)/?$ index.php?pages=$1 [L,QSA]
What should i do to change the url like this:
http://www.example.com/pages/user_profile?username=azzo
to this
https://www.example.com/azzo
I have tryed the following rule:
RewriteRule (?:^|/)([\w-]+)/?$ pages/user_profile.php?username=$1 [L,QSA]
after the
RewriteRule ^(.+?)/?$ index.php?pages=$1 [L,QSA]
but i get blank page.

Facing error after removing .php exetension

This is my .htaccess code after remove .php extension. I'm facing this error
Bad Request Your browser sent a request that this server
could not understand.
DirectorySlash On
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2? [R,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ https://%{HTTP_HOST}/$1/ [L,NC]
EDIT:
.htaccess is located inside the root directory.
The original URL is this: cloud9cumulus.com/shieldpayments/view/login.php
Try this,
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^shieldpayments/view/([\w-]+)$ shieldpayments/view/$1.php [L]
This rule is enough to remove your .php extension for your current url, and remove other rule related to this before trying.
The solution of problem finally get and here it is
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
</IfModule>

rewrite dynamic url after form submit

My link is
search?c=category&s=product
I have already removed the .php with htaccess, everything is working apart from when a user searches, the first page will show the link above, I want to show the link as below:
search/category/product
I tried the below code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^product/(.*)/(.*)/(.*) product.php?id=$1&c=$2&name=$3
RewriteRule ^search/(.*)/(.*)/(.*) search.php?c=$1&s=$2&page=$3
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{THE_REQUEST} \ /+search\?c=([^&\ ]+)&s=([^&]+)
RewriteRule ^ /search/%1/%2? [L,R]
What am I missing in the above?
Have rules this way:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /site/
RewriteCond %{THE_REQUEST} /search\?c=([^&\s]+)&s=([^&\s]+) [NC]
RewriteRule ^ search/%1/%2? [L,R]
RewriteRule ^product/([^/]+)/([^/]+)/([^/]+)/?$ product.php?id=$1&c=$2&name=$3 [L,QSA]
RewriteRule ^search/([^/]+)/([^/]+)/([^/]+)/?$ search.php?c=$1&s=$2&page=$3 [L,QSA]
RewriteRule ^search/([^/]+)/([^/]+)/?$ search.php?c=$1&s=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

htaccess- unable to remove file extension from url php

I am trying the following rules to remove file extension from file name but the problem is it is showing that it is removed but when I login again it is not redirecting to my home page.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase \
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
Thank You
In your redirection just type in page name with no extension, eg. header('Location: index'); or redirect_user('index'); and use the code below in your .htaccess file. now the link on the browser will be say "localhost/index"
RewriteEngine on
RewriteRule ^([^./]{3}[^.]*)$ /index.php?page=$1 [QSA,L]
I think your rules are just a little bit off. You are using THE_REQUEST to redirect php extension to non php extension but you are not using a / after %1 in the rewriterule so when the next rule is read it is not matching because it will only internally redirect if there is a / in that rule. So either add a forward slash after %1 in the second rule or make the backslash optional in the last rule. I would just make it optional in last rule so it will match either way.
Try this update and see how it works.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /([^&\ ]+).php [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/?$ $1.php [NC,L]

Remove .php in url

I have a website in a sub-folder on my webhost. And now I want to remove the .php from the url.
I have this .htaccess right now:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /sub-folder/
# remove index.php from URLs
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ $1 [L,R=302,NC,NE]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
And index works fine. But how do I fix this for other pages, like contact.php, about.php ect. Do I have to add this like all index.php in my .htaccess or how? Please help :)
Thanks!
Right below this rule:
RewriteRule ^index\.php$ - [L]
try adding:
RewriteCond %{THE_REQUEST} \ /+([^\?]+)\.php
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^(.*)$ $1.php [L]

Categories