htaccess rewrite url to hide php question mark - php

my url formatmysite/profile/?theusernamewith hide php extension, im trying to hide question mark in url so the url will like mysite/profile/theusername, looked up few posts about what i should do is add external redirect then internal forward in htaccess, tried lot of code still can't get it work. this is what i have now:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

Keep your .htaccess like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /profile/
# new rules
RewriteCond %{THE_REQUEST} \s/+profile/?(?:index\.php)?\?([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ index.php?$1 [L,QSA]
# php hiding
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Related

htaccess rewrite giving off .php at the end of a paremeter

I have created my own .htaccess file which purpose is to change the username URL of my site (along with remove the .php extension from all urls).
The intent is for the URL to transform from:
www.example.one/profile.php?id=username
to
www.example.one/username
I know this is a common problem with "username" htaccess rewrites, however, none of the answers I have found have helped me solve my problem.
This is my full .htaccess:
Options -Indexes
Options +FollowSymLinks
RewriteEngine On
# only allow rewriting to paths that dont exist
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
# redirect example.one to www.*
RewriteCond %{HTTP_HOST} ^example\.one$ [NC]
RewriteRule ^(.*)$ http://www.example.one [R=301,L]
# no php extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# example.one/username
RewriteRule ^/([a-zA-Z_\-0-9]+)*$ ./profile.php?id=$1
Cheers.
You may use these rules in your site root .htaccess:
Options +FollowSymLinks -Indexes
RewriteEngine On
# redirect example.one to www.*
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /profile\.php\?id=([\w-]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# no php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# example.one/username
# only allow rewriting to paths that dont exist
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ profile.php?id=$1 [L,QSA]

Show correct URL without extension & redirect if URL is incorrect

I've added a bounty of 50 to this question. I'm trying to make it so that:
all website pages show www.example.com/page/ - instead of /page. My current code below is only successful for www.example.com/page
How can the .htaccess code be corrected to achieve this?
Also, changing the canonical URL meta tags from www.mysite.com/page to www.example.com/page/ - will www.example.com/page/ show up in search engines instead of /page ?
The current .htaccess code is:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^ example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Note that the first rewriting conditions just forward to www. and https, so the only part that might need editing might just be:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
So all I want to do it simply add a '/'. A wordpress blog (presumably PHP files rather than HTML) that achieves my wants on the same website (it's at www.example.com/blog/) has htaccess code being:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
I'm not sure how this works but I'd love to learn more about .htaccess and redirects, but nothing much seems to be on the web for beginners. I've also read this post. It also must be https (not sure why but blog posts also can have a http link, whereas other pages redirect to https).
The website also has a blog (php files) on it with its own htaccess file, installed under example.com/blog/ - the htaccess code is the default wordpress one:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
Have your .htaccess as this in site root:
RewriteEngine On
# force www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# force https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# remove .php or .html extensions
RewriteCond %{THE_REQUEST} \s/+(.+?)\.(?:html|php)[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
# add .html extension
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
# add .html extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
For issues with relative paths for css/js/images add this just below <head> section of your page's HTML:
<base href="/" />
This works for me, the top one is excluding .php from the URL and the bottom one excludes .html from the URL.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [NC,L]

Get URI params without index.php

I have a URL like http://example.com/index.php/fruit/1a2b3c
I want to get the URI's for which I have written a code.
Now I want to remove the index.php from the visible URL, it should work even when the URL is http://example.com/fruit/1a2b3c and it should still point to index.php
I am on apache2 using PHP 7
Add following code to your .htaccess file.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
change your .htaccess file to,
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
hope this will work.

Using htaccess to redirect user profiles

I am creating a social networking site. Using .htaccess I have successfully removed .php extensions from urls but I am not able to redirect domain.com/u.php?username=[username] to domain.com/u/[username]. Following is my .htaccess file:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
DirectoryIndex home.php
I have seen many examples and codes in stackoverflow but all of them redirect from domain.com/user.php?username=[username] to domain.com/[username]. But this would affect my website since I have removed .php extensions and I might end up with name clash. Thanks in advance.
You can put this code in your root htaccess
DirectoryIndex home.php
Options -MultiViews
RewriteEngine On
RewriteBase /
# redirect /u.php?username=XXX to /u/XXX
RewriteCond %{THE_REQUEST} \s/u\.php\?username=([^&\s]+)\s [NC]
RewriteRule ^ u/%1? [R=301,L]
# hide php extension (if file exists)
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/(.+)\.php(?:\?|\s) [NC]
RewriteRule ^ %1? [R=301,L]
# internally rewrite /u/XXX to /u.php?username=XXX
RewriteRule ^u/([^/]+)$ u.php?username=$1 [L]
# internally rewrite (if it exists) extensionless to php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+)$ $1.php [L]

site load more time in url rewrite using .htaccess file

Hi I am new to url rewrite help in this process.
Actually I want to rewrite my url from :
http://abcd.com/merchant.php?store=2&area=323&storename=Vendor-2
to
http://abcd.com/Vendor-2
But it's not working. When I click on href target that time it is loading. Below is my .htaccess file
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteRule ^([^/]*)\.html$ /merchant\.php?store=1&area=132&storename=$1 [L]
Above file is my .htaccess file but it's not working.
Can somebody give me a suggestion?
# This should match when storename contains [a-zA-Z0-9_-]
RewriteCond %{QUERY_STRING} storename=([\w-]+)
RewriteRule merchant.php /%1 [R=301,L]

Categories