Convert ugly urls to user friendly urls - .htaccess

In my website there are lot of ugly URLs and now I need to rewrite them to nice user friendly URLs.
These are some of ugly URLs from my website :
http://www.mydomain.com/profiles/tutors/index.php?code=1285&name=Ricky+Pointing
http://www.mydomain.com/profiles/centers/index.php?code=2285&name=City+&+Gills
http://www.mydomain.com/about.php
http://www.mydomain.com/contact.php.... and much more
So I am looking for nice user friendly solution to above ugly URLs, something like this :
http://www.mydomain.com/tutors/Ricky_Pointing.html
http://www.mydomain.com/centers/City_&_Gills.html
http://www.mydomain.com/about.html
http://www.mydomain.com/contact.html
I tried it something similar this in my .htaccess file..
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /profiles/tutors/index.php\?code=([0-9]+)&name=([^&]+)&?([^\ ]+)
RewriteRule ^profiles/tutors/index\.php /%1/%2/?%3 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/(.+)/$ /profiles/tutors/index.php?code=$1&name=$2 [L,QSA]
But still no luck.
Can anybody help me to do this?
Thank you.

I think you might be overcomplicating it with RewriteConds and such. You'll probably have rules like this:
RewriteRule ^about\.html$ about.php [L]
RewriteRule ^contact\.html$ contact.php [L]
RewriteRule ^tutors/(.*)\.html$ /profiles/tutors/index.php?name=%1 [L]
RewriteRule ^centers/(.*)\.html$ /profiles/centers/index.php?name=%1 [L]
You'll then have to modify your PHP scripts to look up the appropriate thing with that name without relying on having the code present. It'll also have to deal with the presence of underscores as existed in the original URL.

Related

How do I redirect old ugly looking urls to new SEO friendly urls?

I really need some help figuring this out since I've been trying to solve this problem for couple of days already.
I had old urls that looked like this:
category.php?id=6&name=maxi-dresses&type=1**
I have changed them to look like this using htaccess: https://markandroberts.com/category/dresses/maxi-dresses/
As you can noticed, in the new urls I have also changed the position of the category id by putting it at the end instead at the beginning.
What I want to do now is to 301 redirect all the old category urls that are already indexed in Google to the new location.
Here's my current htaccess code:
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.markandroberts.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^product/([0-9]+)/(.*)/(.*)/$^ product.php?id=$1&type=$2&name=$3
RewriteRule ^product/([0-9]+)/(.*)/(.*)/ product.php?id=$1&type=$2&name=$3
RewriteRule ^category/(.*)/(.*)/$^ category.php?type=$1&id=$2
RewriteRule ^category/(.*)/(.*)/ category.php?type=$1&id=$2
RewriteRule ^type/(.*)$/^ main-category.php?maincat=$1
RewriteRule ^type/(.*)/ main-category.php?maincat=$1
RedirectMatch 301 /category.php?id=$1&name=$2&type=$3 /category/$3/$1/
Any solutions that work?
You need to modify your .htaccess file to include some "pretty url" rules. Here is an example for you.

Using .htaccess rewrites to treat variables as folder names

I want to use .htaccess rewrites to create the illusion of each user having their own folder on a website, when it's really just a handful of PHP files that generate all user content (often by pulling from a database):
/members/bob/avatar.jpeg points to /members/avatar.php?username=bob
/members/bob/about.html points to /members/about.php?username=bob
I'm completely lost on how to do this... I've pulled up tutorials on using the htaccess rewrite mechanics, but none of them involve changing /folder_name/variable/file_synonym to /folder_name/file_actual?var=variable. If anyone knows how to do something like that I'd be really interested, because I have no idea where to even start on this.
Try:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+members/avatar\.php\?username=([^&\ ]+)
RewriteRule ^ /members/%1/avatar.jpeg? [L,R]
RewriteCond %{THE_REQUEST} \ /+members/about\.php\?username=([^&\ ]+)
RewriteRule ^ /members/%1/about.html? [L,R]
RewriteCond ${REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^members/([^/]+)/(avatar|about)\.(jpeg|html)$ /members/$2.php?username=$1 [L]
This ended up being the solution:
RewriteEngine On
RewriteRule ^members/([A-Za-z0-9-]+)/avatar.jpeg?$ members/avatar.php?username=$1 [NC,L]
RewriteRule ^members/([A-Za-z0-9-]+)/about.html?$ members/about.php?username=$1 [NC,L]
RewriteRule ^members/([A-Za-z0-9-]+)/feed.html?$ members/feed.php?username=$1 [NC,L]
I used http://htaccess.madewithlove.be/ to test my work as I went along, to make sure that I was doing everything right. Apparently internal htaccess rewrites aren't allowed on Hostgator's shared servers, so I'll have to wait until I upgrade to really use this method.

How to use RewriteCond to get rid of duplicate page

Hello guys! I need some help with htaccess, because I'm really stuck with it. I have some pages, like:
site.ru/books
site.ru/pens
And those links can be called by other urls:
site.ru/index.php?show=books
site.ru/index.php?show=pens
I hope you understand those links above are equal. Just, urls are user friendly.
I want to use RewriteCond to make a redirect from "site.ru/index.php?show=books" to "site.ru/books", because Google or other Search Engines don't like duplicate pages.
But the problem is I don't know how to write this… Help me please, and sorry for my bad English ;-)
put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+index\.php\?show=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?show=$1 [L,QSA]

htaccess clean url for Joomla component

I give up. I have read through dozens of questions on here, even asked my own, I have tried numerous things, I just don't know what to do anymore.
I need to create url's out of the following formats: (NSFW links, be warned)
http://jbthehot.com/#page=1
http://jbthehot.com/home?func=viewphoto&id=2969
http://jbthehot.com/home?page=4
http://jbthehot.com/home?func=viewalbum&aid=75
I must be retarded because I simply do NOT understand htaccess. I try not to ask for all-done answers, but in this case, an actual clear, copy/paste answer for at least one of the above formats would really be helpful.
I repeat, I have tried a lot of things, so I'm obviously doing something wrong, or I'm not understanding the rules and conditions right, so a copy/paste answer that is SURE to work would really help me understand what I'm doing wrong here.
If it helps, the component used here is SIM Gallery, on Joomla and there are a couple of other rewrite rules in effect in the .htaccess , here they are:
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
#
Thank you for any help
RewriteEngine On
RewriteBase /
RewriteRule ^home/viewphoto/([0-9]+)$ home?func=viewphoto&id=$1 [L]
RewriteRule ^home/viewalbum/([0-9]+)$ home?func=viewalbum&aid=$1 [L]
RewriteRule ^home/page/([0-9]+)$ home?page=$1 [L]
So http://jbthehot.com/home/viewphoto/2969 would show you http://jbthehot.com/home?func=viewphoto&id=2969, etc

Rewrite specific URL to different directory

Im trying to redirect this,
example.com/slide?page=4 to example.com/slide/issue43?page=4
But it cannot effect other URL's like, example.com/slide/issue57?page=4
Im really stuck, these regular expressions are so weird. Here's the rewriterule that I've come up with,
This is not working
RewriteRule ^slide?page(.*)$ http://example.com/slide/issue43?page=$1 [L,R=301]
I need to target 'slide?page=X' specifically and have it redirect or point to 'slide/issue43?page=X'
This should work for you:
RewriteCond %{REQUEST_URI} ^/slide$
RewriteCond %{QUERY_STRING} ^page=(.*)$
RewriteRule ^(.*) /slide/issue57?page=%1 [R=301,L]

Resources