I am using htaccess for generating clean urls in my project.
Like, I want to convert this url :
https://localhost/erp/profile.php?username=kopyo
to this url:
https://localhost/erp/profile/kopyo
My htaccess code is:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /erp
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# Code I am using is this line but not working
RewriteRule profile/(.*) profile.php?username=$1
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Let me know what I am doing wrong in this. Thanks
Make sure to use proper flags after RewriteRule and re-arrange your rules:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /erp/
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,NE,L]
RewriteRule ^profile/(.+)$ profile.php?username=$1 [L,NC,QSA]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Related
My Original URL
http://www.mcoh.co.in/blog-single?category=Blog&id=Janurary%202018%20Product%20training%20Scores
and Expecting in:
http://www.mcoh.co.in/mcohBlog/Janurary%202018%20Product%20training%20Scores
But Am receiving same url as original.
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^mcoh([^/]*)/([^/]*)\$ /blog-single?category=$1&id=$2 [L]
Your problem with this line :
RewriteRule ^mcoh([^/]*)/([^/]*)\.php$ /blog-single?category=$1&id=$2 [L]
replace it with :
RewriteRule ^mcoh([^/]*)/([^/]*)$ /blog-single?category=$1&id=$2 [L]
The extension is already removed by previous rule .
Moreover , your code should look like this :
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteRule ^mcoh([^/]*)/([^/]*)$ /blog-single?category=$1&id=$2 [L,NE]
RewriteEngine on
RewriteRule ^/?mcohBlog/([^/d]+)/?$ blog-single.php?category=Blog&id=$1 [L,QSA]
Can you try this? Just add this rewriterule at the end of your code. and remove your last line of code
EDIT
Also your code organization is very wrong. That's why it's not working as expected
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteRule ^/?mcohBlog/([^/d]+)/?$ blog-single.php?category=Blog&id=$1 [L,QSA]
This should be your last code
To redirect/rewrite
http://www.mcoh.co.in/blog-single?category=Blog&id=Janurary%202018%20Product%20training%20Score
to
http://www.mcoh.co.in/mcohBlog/Janurary%202018%20Product%20training%20Scores
put the following at the top of your htaccess file
RewriteEngine on
#To externally redirect /blog-single?category=foo&id=bar to /mochBlog/bar
RewriteCond %{QUERY_STRING} ^category=([^&]+)&id=(.+)$ [NC]
RewriteRule ^.*$ /mochBlog/%2? [R,L]
#To internally redirect /mochBlog/bar to /blog-single?category=foo&id=bar
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^mochBlog/(.+)$ /blog-single?category=blog&id=$1 [L]
I have this htaccess and it almost is working, but..
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.loggs\.no [NC]
RewriteRule ^(.*)$ https://loggs.no/$1 [L,R=301]
RewriteCond %{THE_REQUEST} \s/+show(?:\.php)?\?id=([0-9]+) [NC]
RewriteRule ^ show/%1? [R,L]
RewriteRule ^show/([0-9]+)/?$ show.php?id=$1 [L,QSA]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
But it doesn't add trailing slashes to all my pages and when I'm clicking on show/6 and then will go to e.g. Show/3 it goes until show/show/3
and I would like to remove .php for all my pages too.
You can use these rules in site root .htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(loggs\.no)$ [NC]
RewriteRule ^(.*?)/?$ https://%1/$1/ [L,R=301,NE]
RewriteCond %{THE_REQUEST} \s/+(show|feeds|links)(?:\.php)?\?id=([0-9]+) [NC]
RewriteRule ^ %1/%2/? [R=301,NE,L]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,NE,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
RewriteRule ^(show|feeds|links)/([0-9]+)/?$ $1.php?id=$1 [L,QSA,NC]
RewriteRule ^(show|feeds|links)/?$ $1.php [L,NC]
I´m new to .htaccess and I am having problems in rewriting some URLs.
My website is made with php and has 2 parameters: param1 and param2.
The URLs look like similar to:
www.website.com/index.php?param1=12345678
www.website.com/index.php?param1=09876543
www.website.com/index.php?param2=abcdefgh
www.website.com/index.php?param2=qwertzui
I would like to create a .htaccess file to remove “index.php”, replace param1 and param2 with 2 names and add “.html” at the end, so they became:
www.website.com/budget/12345678.html
www.website.com/budget/09876543.html
www.website.com/user/abcdefgh.html
www.website.com/user/qwertzui.html
I have got this code (copied from internet).
It removes the .php extension but in the internally forward it rewrite it at the end of the URL neglecting the parameters.
Is someone so kind to help me to write the code?
Thanks :)
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
You can try:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# handle ?param1=...
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?param1=([^\s&]+) [NC]
RewriteRule ^ /budget/%1.html? [R=301,L,NE]
# handle ?param2=...
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?param2=([^\s&]+) [NC]
RewriteRule ^ /user/%1.html? [R=301,L,NE]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,NE,L]
RewriteRule ^budget/([^/.]+)/?$ index.php?param1=$1 [L,QSA,NC]
RewriteRule ^user/([^/.]+)/?$ index.php?param2=$1 [L,QSA,NC]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
i have one url in my project like
hall/description_banquethall.php?hall_id=1 and my url is working like hall/description_banquethall/1
by using this .htaccess code
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /hall
## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]
## hide .php extension snippet
# To externally redirect /dir/foo.php?id=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+) [NC]
RewriteRule ^ %1/%2/? [R,L]
# To internally forward /dir/foo/12 to /dir/foo.php?id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?hall_id=$2 [L,QSA]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]
now i want to redirect my another url hall/booking.php?hall_id=1&booking_date=2014-10-23&session=Morning to hall/booking/1/2014-10-23/Morning
Below ## hide .php extension snippet line add these 2 new rules:
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+)&booking_date=([^&\s]+)&session=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3/%4? [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/([^/]+)/([^/]+)/([^/]+)/?$ $1.php?hall_id=$2&booking_date=$3&session=$4 [L,QSA]
I have a code in my htaccess that removes the .php from every page on my site. The following code is as far as i came, but what i actually want is that every page has an slash after the name (still without php)
and that de index(.php) name is completely removed.
Icouldn't find something on the internet having these two combined. I did find a code that created a slash at the end but that created many problems with internally redirecting.
Is all of this possible and if so, how?
Many thanks in advance,
Paul
Options +FollowSymLinks -MultiViews
RewriteEngine On
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Try:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ /%1/ [R,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)index\.php [NC]
RewriteRule ^ /%1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+?)/?$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]