SEO Friendly URL Command in .htaccess - .htaccess

I have a dynamic page that looks like the following:
www.sitedomain.com/page.php?id=30&name=about_our_company
I want to make my website links SEO friendly by having something like the following:
www.sitedomain.com/about_our_company.html
Or
www.sitedomain.com/about_our_company
My question is: what regex/code I should have in the .htaccess file?
Thanks

This ofc has a fixed id of 30.
RewriteRule ^about_our_company/?$ /page.php?name=$1&id=30 [NC,L]

since /about_our_company doesn't include the ID, it's impossible to invent the ID correctly.
the way I do it in my own CMS is to have something like this in the .htaccess:
RewriteEngine on
RewriteRule ^(.*)$ /index.php?page=$1 [QSA,L]
then in index.php, use the $_REQUEST['page'] (assuming PHP) variable to find the right page details in the database

You could improve this to be more generic:
RewriteRule ^([^/]+)/([^/]+)?$ /page.php?id=$1&name=$2 [NC,L]
The following URL's would all match:
http://example.com/30/about_our_company
http://example.com/29/contact
http://example.com/10/our_work

Related

htaccess is working but does not replace the url

I'm trying to modify the subdomain name in the URL to make it look nicer. My current URL look something like:
www.mystore.com/productInfo.php?cPath=11_11&productID=222
So, I want to make it nicer by Rewrite in .htaccess in main with this code:
RewriteEngine On
RewriteRule ^productInfo/([0-9_-]+)/([0-9_-]+) productInfo.php?cPath=$1&productID=$2 [NC,L]
When I run and test it on the URL by typing www.mystore.com/productInfo/11_11/222 in the URL it works well. However, when this page is redirected by a different page or is 'refreshed' with a self redirecting a href= link(in which the link is written in php by the previous programmer), the above old link is still visible in the URL instead of the new one.
I am still a beginner while I suspect that I might need to change something in the cPanel/Apache(I think) for this but currently, I am still do not have access to the cPanel control. Is there anything that I might have missed to write in the .htaccess file or I really do need the cPanel control or any other reasons?
Any help is appreciated. Sorry that I could not find similar questions on this.
You can use the following :
RewriteEngine On
RewriteBase /
#redirect /productInfo.php?cPath=foo&productID=bar to /productInfo/foo/bar
RewriteCond %{THE_REQUEST} /productInfo\.php\?cPath=([0-9_-]+)&productID=([0-9_-]) [NC]
RewriteRule ^ productInfo/%1/%2? [L,R=301]
#rewrite new URL to the old one
RewriteRule ^productInfo/([0-9_-]+)/([0-9_-]+) productInfo.php?cPath=$1&productID=$2 [NC,L]

.htacess url rewrite rule to shortern the url and remove "?st_id=" from it

I currently goto:
support-requests/?st_id=7
Ideally i would like to just go straight here using .htaccess url-rewriting to make the page neater to look at and navigate to without the "?st_id=" being displayed in the url.
support-requests/7
This is what i currently have but i know its wrong and it does not work:
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule support-requests/ support-requests/%1/%2/?
Could i have some assistance in getting this correct please?
Thanks
Use this:
RewriteEngine On
RewriteRule ^support-requests/([^/]*)$ /support-requests/?st_id=$1 [L]
Should leave you with:
www.example.com/support-requests/7

Using .Htaccess url redirection

I want to rewrite my url but i cant do proper way.
mysitename.net/index.php?title=Category:Public_Companies&pagefrom=8
To :
mysitename.net/Category:Public_Companies/8
How can i do that.
If you have other pages than Category, I recommend to use this:
RewriteRule ^Category:(.+)/(.+)$ /index.php?title=Category:$1&pagefrom=$2 [L]
Otherwise, use this:
RewriteRule ^(.+)/(.+)$ /index.php?title=$1&pagefrom=$2 [L]

rerwrite url in .htaccess

I want to make the old urls point to the new pages. I'm having trouble because the old URL looks like this: http://nilandsplace.com/store/camping_eng/coghlans-campfire-cooking-forks-toaster-forks-package-of-4.html.
I want to do a URL re-right in .ht access from mywebsite.com/camping_eng/some.html to mywebsite.com/camping/some.html, were the "some.html" part is identical.
Only camping_eng changes to camping.
You might want to try this:
RewriteEngine on
RewriteRule ^camping_eng/([a-zA-Z0-9-=_.]+)/?$ http://mywebsite.com/camping/$1 [L,R=301]
This example works according to your question:
from mywebsite.com/camping_eng/some.html
to mywebsite.com/camping/some.html were the "some.html" part is identical.
RewriteEngine on
RewriteBase /store
RewriteRule ^camping_eng/(.*)$ camping/$1 [QSA,L,R=301]

httacces Rewrite URLs

I wanted to rewrite some urls from
www.example.ro/women_shoes.php to www.example.ro/women-shoes/
and with pagination looks like
www.example.ro/women_shoes.php?page=1 to www.example.ro/women_shoes/pagina-2/
www.example.ro/men_shoes.php to www.example.ro/men-shoes/
with pagination like the first one
www.example.ro/sandale_barbati.php to www.example.ro/sandale-barbati/
and so on..
for the last example i tryed
RewriteRule ^sandale-barbati/pagina-([0-9]+)/ /sandale-barbati.php?page=$1 [L]
RewriteRule ^sandale-barbati/?$ /sandale_barbati.php?$
but i need to write that for every page...
is there a way to do that automaticaly for all pages ?
thanks
If you always use the same naming for all your pages, this will work:
RewriteRule ^([a-zA-Z]+)([-_]){1}([a-zA-Z]+)/pagina-([0-9]+)/$ $1_$3.php?page=$4
RewriteRule ^([a-zA-Z]+)([-_]){1}([a-zA-Z]+)/$ $1_$3.php
RewriteRule ^([a-zA-Z_]+)/pagina-([0-9]+)/$ /$1.php?page=$2
RewriteRule ^([a-zA-Z_]+)/$ /$1.php
You can surf to /women_shoes/ and it will be the /women_shoes.php page.
You can surf to /women-shoes/ and it will be the /women_shoes.php page.
You can surf to the /shoes/ and it will be the /shoes.php page.
I hope this has met all your requirements and was of sufficient help to you. For future reference, the website http://www.regular-expressions.info/ is very handy when dealing with RegEx (such as .htaccess rewrite rules).

Resources