Replace Ugly URLs To Pretty And SEO-Friendly - .htaccess

I'm looking for a .htaccess Rewrite URL solution to convert my web application's ugly URL to Pretty one.Previously I have converted an URL localhost/example/user.php?u=username to localhost/example/username with the following .htaccess code
RewriteRule ^([^/\.]+)/?$ user.php?u=$1
now I want to convert the following URL:
localhost/example/messages.php?u=username ----> localhost/example/messages
Thanks.

Keep these 2 rules in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^([^/.]+)/?$ user.php?u=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/.]+)/?$ /$1/user.php?u=$2 [L,QSA]

Related

Seo friendly url - Unable to find a way to get the htaccess to rewrite the urls

Currently, my site has this url format:
https://example.com/blog/news.php?read=article-title-here
I would like to change this url to:
https://example.com/news/article-title-here
Any idea how to do this please? I tried this method but see no changes in the browser bar(even after emptying the cache):
RewriteCond %{QUERY_STRING} ^read=(.*)$
RewriteRule ^news/?$ %1.php? [R=301,L]
Also, where should the htaccess file be written?
in https://example.com/
or
https://example.com/blog/
Thank you in advance!
Ben
You can use in your https://example.com/.htaccess:
Options -MultiViews
RewriteEngine on
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+blog/news\.php\?read=([^\s&]+) [NC]
RewriteRule ^ /news/%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^news/(.+?)/?$ blog/news.php?read=$1 [L,QSA,NC]

.htaccess : write rule with langage parameter for SEO

The goa is to rewrite:
www.example.com/fr/something.php ->www.example.com/something.php?lang=fr
I tried with:
RewriteRule ^.*/fr/(.*)$ http://www.example.com/$1?lang=fr[R,L]
RewriteRule \/fr\/(.*)$ http://www.example.com/$1?lang=fr[R,L,QSA]
RewriteRule /fr/(.*)$ http://www.example.com/$1?lang=fr[R,L,QSA]
Not good:
Any idea ?
Try with:
RewriteEngine on
RewriteRule ^fr/(.*)$ /$1?lang=fr [QSA,L]
I do not think you want a redirect, but only a rewrite (without changing the url)

.htaccess Rewrite rule with multiple parameters

I want Rewrite rule for .htaccess for change URL like below..
From
http://www.example.com/templates/professional_website/news.php?ac=post&id=2&p=1
To
https://www.example.com/templates/professional_website/news/post/2/1
Untested answer taken from a random linked question:
RewriteEngine on
RewriteRule ^templates/professional_website/news/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ templates/professional_website/news.php?ac=$1&id=$2&p=$3 [L,NC,QSA]
RewriteRule ^templates/professional_website/news/([^/\.]+)/([^/\.]+)/?$ templates/professional_website/news.php?ac=$1&id=$2 [L,NC,QSA]

.htaccess rewriting GET

I've never needed to use a .htaccess before and I'm fairly new to coding I'm trying to get localhost/index.php?id=123456789 to be passed as localhost/123456789/ but I just cant get the HTaccess right, i've tried everything I could find from prevoius posts here, haha!
My current HTACCESS looks like this, however it doesnt do what I want.
RewriteEngine On
RewriteRule ^id/(\d+)$ /index.php?id=$1 [NC,QSA,L]
You can do it with mod_rewrite in .htaccess however I'm not sure from your question which direction you want it to be passed to.
If you want people to type the php script in the URL bar you want:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/
Or if you want people to enter the "pretty" version but for your server to load the PHP script you need:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING}
For both ways and 301 redirect to pretty URL:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING} [L]
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/ [L,R=301]

change the url using with htaccess

i am trying to change the url using htaccess rewrite
i want to change this url
page/details.php?name=abcdef&id=18
to
page/abcdef
Here my sample code for this
RewriteEngine On
RewriteRule ^company/([A-Za-z0-9-]+)/$1 company/details.php?name=$1&id=$2 [R=301,L]
this is not working, and also i was tried many code but not working,please help me to find htaccess code for this url
Thanks advance
This should do what you're after:
RewriteCond %{QUERY_STRING} ^.*name=([a-zA-Z0-9]*).*$
RewriteRule ^page/(.*)$ page/%1? [R=301,L]
Try this one:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^name=(.*)&id=(.*)$
RewriteRule ^page/(.*)$ /page/%1? [R=301,L]
This will check for these 2 parameters in the query string.

Resources