convert dynamic url to seo friendly url in htaccess - php

Cuurent URL is
https://www.website.com/colleges.php?id=NR%20School%20of%20Architecture
we want to change this URL into SEO friendly URL as like,
https://www.website.com/colleges/NR-School-of-Architecture
we used this htaccess,but only the spaces were removed
RewriteEngine On
RewriteBase /
# replace all space by hyphen
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ %1-%2 [L,NE,R=302]
we used this htaccess to remove .php, it worked and .php was removed in the entire site.
But we dint get a seo freindly url like this,
colleges/NR-School-of-Architecture
RewriteBase /
## 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]
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]
kindly help us in achieving seo friendly url using htaccess
Thanks in advance.

You will need to define the rewrite rules in a .htaccess file placed in your site document root location after you have confirmed that mod-rewrite is enabled
RewriteEngine On
RewriteBase /
RewriteRule ^home /index.php

Related

301 redirection instead of 302 redirection

I have used this code so far in my local (it's working fine).
RewriteEngine On
Options -Indexes
RewriteBase /projectname/
## 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]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
redirect 301 /projectname/index/ http://localhost/projectname/
redirect 301 /projectname/about/index/ http://localhost/projectname/about/
redirect 301 /projectname/agent/index/ http://localhost/projectname/agent/
redirect 301 /projectname/privacy/index/ http://localhost/projectname/privacy/
RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R]
I place this in root .htaccess file. But i have three subfolders like about, agent, privacy also.
I used this code for
remove .php extension (except index pages in all folders include root).
add trailing slash
remove more than one trailing slashes
remove index.php in all folders (including root and subfolders).
(Note: Same thing applied for all pages inside subfolder as well)
But these pages redirected from 302 and then redirect 301.
I need to redirect only 301 include all the options specified above (points).
Any one please amend these redirection rules.
Any one please help me ?
Your htaccess could be simplified this way in order to respect your criteria
DirectoryIndex index.php
Options -MultiViews
RewriteEngine On
RewriteBase /projectname/
# redirect "*/index.php" to "*/"
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/(.+/)?index\.php\s [NC]
RewriteRule ^ %1 [R=301,L]
# redirect "*/page.php" to "*/page/"
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/(.+)\.php\s [NC]
RewriteRule ^ %1/ [R=301,L]
# rewrite back "*/page/" to "*/page.php"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+)/$ $1.php [L]

write .htaccess rewrite website URL multiple parameters

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]

Pretty URL htaccess

I have the below url that I want to prettify. Any help appreciated.
From;
http://www.example.co.uk/search-menu?r_id=1&name=testname
To
http://www.abklab.co.uk/search-menu/1/testname
I have tried below but getting Internal Server Error
# external redirect from actual URL to pretty one (WEB)
RewriteCond %{THE_REQUEST} \s/+search-menu(?:\.php)?\?r_id=([^\s&]+)&name=([^\s&]+) [NC]
RewriteRule ^ /search-menu/%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one (WEB)
RewriteRule ^search-menu/([\w-]+)/([\w-]+)/?$ search-menu.php?r_id=$1&name=%2 [L,QSA,NC]
FYI: Below rule removes .php extension site wide;
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
Try this in your htaccess :
RewriteEngine on
RewriteCond %{THE_REQUEST} /search-menu\?r_id=([^&]+)&name=([^\s]+) [NC]
RewriteRule ^ %1/%2? [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search-menu/([^/]+)/([^/]+)/?$ search-menu.php?r_id=$1&name=$2 [NC,L]
Found something for you - the wizard will help you make your URL perfect
http://www.generateit.net/mod-rewrite/index.php
Add this line to your .htaccess file. It will take the variables and make is nice and pretty:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.html$ /search-menu?r_id=$1&name=$2 [L]
Keep in mind this is basic and you will need to modify it to your addresses but it is just to give you the idea of what you need to do.

htaccess mod_rewrite rules combination issue

I'm so newbie in apache, so it's a basic question but I couldn't solve this using another questions or links.
I tried to change my URL using .htaccess and I had two purposes;
hide .php extension
change some queryString from somefile.php?id=bar to somefile/id/bar which bar is a number or mixed string like T51-3.
My queryString is http://localhost/payment/theme/ticket?id=770314 that I want change it to http://localhost/payment/theme/ticket/id/770314 or
http://localhost/payment/theme/ticket/770314
I found this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# 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 [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
in stackoverflow.com/this_question
This was working nice but didn't solve my second issue.
So, I searched for a while in this site and others and did find some sample codes like this one :
RewriteRule ^([a-zA-Z0-9_-]+)-([0-9]+)$ ticket.php?id=$2
or
RewriteRule ^(.*)$ /ticket.php?url=$1 [L]
and etc...
but none of these didn't work for me.
Some of them will make the server done and some don't have any affect at all.
Would you please guide me to right way...
I looked at these questions:
htaccess rewrite for query string
how to change query string parameters with name using .htaccess?
How can I use .htaccess to hide .php URL extensions?
And these links:
http://simonecarletti.com/blog/2009/01/apache-query-string-redirects/
https://wiki.apache.org/httpd/RewriteQueryString
UPDATE :
This is my site strucure:
There is an htaccess for important folders, like, forms, db, classes etc...
All my site pages are in the theme folder, as you can see in the picture.
In the root htaccess, there's just two lines that auto start sessions.
And I added your code to theme's htaccess.
Thanks in Advance
You can use this code in
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /payment/theme/
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php\?(\w+)=([^\s&]+)\s [NC]
RewriteRule ^ %1/%2/%3? [R,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# to handle http://localhost/payment/theme/ticket/id/770314
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)/([\w-]+)/([\w-]+)/?$ $1.php?$2=$3 [L,QSA]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

hide.php files with variables htaccess

i have a problem on my htaccess file , i'm trying to hide all .php extensions on my website,
i tried many htaccess codes but most of it gives me errors , 404 Not Found , most of my files contain variables like ?id and ?category , index/?cat=Action , i want htaccess to hide php extension and forward /dir/foo to /dir/foo.php , help me pls :)
RewriteEngine On
RewriteBase /
# turn off index.php for home page
RewriteRule ^index\.php/?$ / [L,R=301,NC]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
You need to write php to the end of urls not the other way around. You can write variables like this: href="foo?bar=1". This code has worked for me:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
You can use this code in your root .htaccess:
RewriteEngine On
RewriteBase /
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

Categories