.htaccess redirection rule for site - .htaccess

I need help with htaccess URL rewriting for my site.
Problem is that just first page in some category has nice cruft free URL, and all other pages has dirty URLs.
For example first page has link like this:
http://www.my-site-name.com/Category-name/Subcategory-name/
But page 2, and any other pages are like this:
http://www.my-site-name.com/showcat.php?cat=Category-name&subcat=Subcategory-name&page=2
http://www.my-site-name.com/showcat.php?cat=Category-name&subcat=Subcategory-name&page=3
So, I need some SEO friendly htaccess redirect rule to make URLs like this:
http://www.my-site-name.com/Category-name/Subcategory-name-page-X/
or like this
http://www.my-site-name.com/Category-name/Subcategory-name/X/
But probably I would like first more.
This is part of .htaccess for that: (I just added full content, maybe there is something more about it.
## For top rated items
RewriteRule ^top/page/(.*)/$ top.php?page=$1 [L]
## For latest items
RewriteRule ^latest/recent-page-(.*)/$ latest.php?page=$1 [L]
## For show most rated - most clicked - most downloaded and most searched items
RewriteRule ^most-rated.html$ showmost.php?type=1 [L]
RewriteRule ^most-clicked.html$ showmost.php?type=2 [L]
RewriteRule ^most-downloaded.html$ showmost.php?type=3 [L]
RewriteRule ^most-Searched.html$ showmost.php?type=4 [L]
## For showing category of item
RewriteRule ^(.*)/$ showcat.php?cat=$1 [L]
## For showing subcategory of item
RewriteRule ^(.*)/(.*)/$ showcat.php?cat=$1&subcat=$2 [L]
## For showig item
RewriteRule ^(.*)/(.*)/(.*).html$ show.php?cat=$1&sub_cat=$2&img=$3&rewrite=true [L]
## this section should be inserted just after the showing item rule above
#if the query string has cat, sub_cat and Img
RewriteCond %{QUERY_STRING} ^cat=(.+)&sub_cat=(.+)&img=(.+)$ [NC]
#and it is for resource show.php, then 301 redirect to Keyword rich URL
RewriteRule ^show\.php$ http://www.my-site-name.com/%1/%2/%3.html? [NC,L,R=301]

I also cleaned it up a little
#don't rewrite if file or dir exists
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [L]
## For showing category of item
RewriteRule ^([^/]*)/$ showcat.php?cat=$1 [L]
## For showing paged subcategory of item
RewriteRule ^([^/]*)/([^/]*)-page-([0-9]+)/$ showcat.php?cat=$1&subcat=$2&page=$3 [L]
## For showing subcategory of item
RewriteRule ^([^/]*)/([^/]*)/$ showcat.php?cat=$1&subcat=$2&page=1 [L]
## For showig item
RewriteRule ^([^/]*)/([^/]*)/([^/]*).html$ show.php?cat=$1&sub_cat=$2&img=$3&rewrite=true [L]

Related

301 redirect of rewritten pages

I'm moving an existing projet from root to language folder /fr, in order to create a different english project located in /en.
The project is simple, it consists on two pages (index.php and liste.php).
The first page was receiving a parameter
/index.php?hash=[page-hash]
and was rewriten in the .htaccess as
/[page-hash].html
So here is the content of the old .htaccess:
RewriteEngine On
######### Page rewriting ########
RewriteRule ^liste\.html$ liste.php [QSA,L]
RewriteRule ^(.*)\.html$ index.php?hash=$1 [QSA,L]
I want the old rewritten pages (www.domain.com/[page-hash].html), which are indexed by Google, to be 301 redirected to www.domain.com/fr/[page-hash].html so that Google still find them at new new location and is informed it has been moved.
It's not a rewriting, it's a redirect, and I don't know how to do that. I guess I have to use RewriteCond and [R=301] but I don't know how.
Of course the new pages must also be rewritten (/fr/index.php?hash=[page-hash] into /fr/[page-hash].html), and the root generally redirected to /fr. So I already made this new .htaccess:
RewriteEngine On
######## Redirect from root to /fr #######
RewriteRule ^$ /fr
Redirect 301 /liste.html /fr/liste.html
Redirect 301 /liste.php /fr/liste.html
######### Page rewriting ########
RewriteRule ^fr/liste\.html$ /fr/liste.php [QSA,L]
RewriteRule ^fr/(.*)\.html$ /fr/index.php?hash=$1 [QSA,L]
RewriteRule ^en/list\.html$ /en/list.php [QSA,L]
RewriteRule ^en/(.*)\.html$ /en/index.php?hash=$1 [QSA,L]
You're pretty close. You can condense those and you only need QSA (and B) when your substitution contains a query.
RewriteEngine On
######## Redirect from root to /fr #######
RewriteRule ^$ /fr/ [L,R=301]
RewriteRule ^([^/]+)\.(?:html|php)$ /fr/$1.html [L,R=301]
######### Page rewriting ########
RewriteRule ^(fr/liste|en/list)\.html$ $1.php [L]
RewriteRule ^(fr|en)/([^/]+)\.html$ $1/index.php?hash=$2 [L,B,QSA]
Here is the complete answer. The order is important.
RewriteEngine On
######### Page rewriting ########
RewriteRule ^fr/liste\.html$ /fr/liste.php [L]
RewriteRule ^fr/(.*)\.html$ /fr/index.php?hash=$1 [L,B,QSA]
RewriteRule ^en/list\.html$ /en/list.php [L]
RewriteRule ^en/(.*)\.html$ /en/index.php?hash=$1 [L,B,QSA]
################ Redirect from root to /fr ################
RewriteRule ^$ /fr [L,R=301]
RewriteRule ^liste\.html$ /fr/liste.html [L,R=301]
RewriteRule ^(.*)\.html$ /fr/$1.html [L,R=301]
This can still be condensed (see Walf's answer).

Product page with mod_rewrite htaccess

I am working in a catalog store, so far i solved the url rewrite for the main page of the store (store.php) and the categories pages (category.php), this is what i got so far:
RewriteCond %{THE_REQUEST} /category-store\.php\?catid=([a-zA-Z0-9_-]+) [NC]
RewriteRule ^/store/%1? [R=302,L,NE]
RewriteRule ^store/([a-zA-Z0-9_-]+)/?$ category.php?catid=$1 [QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
So the store page is
www.example.com/store
And a category page is
www.example.com/store/furniture
I try to break the products page i want it turn this:
www.example.com/product.php?id=1
into
www.example.com/store/wood-chair-1
This is what i got that doesn't work...
RewriteRule ^store/(.*)-([0-9]+)$ product.php?id=$2 [QSA]
(.*) as product title - ([0-9]+) as product id
This is link example in php:
'. $row['prod_title'] . '

Redirect 404 to search page

Becouse in google webmasters I have 550.000 not found pages - 404 - I want to make some redirections to the search page.
To be userfoul for the users, need to go to the search page, but is realy complicated for me, becouse I need to EXTRACT the search phrase from the 404 url:
Here are the 2 type of url with 404 errors, and need to be redirected.
DOMAIN/movie/12298295-Iron-Man-3
DOMAIN/serie/15108-The-Walking-Dead/seasons/3/episodes/10
Of corse, I need to extract JUST THE NAME of the movie, or the serie and redirect to this url
DOMAIN/search?q=Iron-Man-3
DOMAIN/search?q=The-Walking-Dead
Well this is the URL by parts:
For "movies" just have 4 parts:
DOMAIN - of corse, is the domain name of the website
/movie/ - is created based on the content type (movie or serie)
12298295- is the ID for this item and can be form - 1 - to
infinite
Iron-Man-3 - the name of the movie (is wath I want to get)
For "series" - THE SAME like for the movies, but at the end can have more parts, and the full URL can be: (all this url, for an normal page with NO 404, are ok)
DOMAIN/serie/15108-The-Walking-Dead/seasons/3/episodes/10 - Link to episode page
DOMAIN/serie/15108-The-Walking-Dead/seasons/3/ - Link to some season page
DOMAIN/serie/15108-The-Walking-Dead/ - Link to the TOP page for this serie
Please note, I need to redirect this url, JUST if the result is an 404 page (ErrorDocument 404), becouse the same type of url are used in all pages.
Here is my actual .htacces
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=301,NC,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=301,NC,NE]
RewriteCond %{QUERY_STRING} ^q= [NC]
RewriteRule ^busqueda\b /busquedas [R=301,L,QSA]
# Redirect Trailing Slashes...
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Many thanks for your help
The following shall work for both movies and series:
RewriteRule ^(?:movie|serie)/\d+-([^/]+) /search?q=$1 [R=301,L,NC]

Mod_rewrite - redirect old urls to the new ones

I'm looking for a solution to redirect old urls (e. g. aboutme.php) to the new ones (same /about-me).
The problem is: if I'll go to example.com/aboutme.php, the user is not redirected to pretty url (/about-me). Adding R=301 doesn't help - it makes /about-me redirect to aboutme.php.
Here's my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^(.*) http://example.com/$1 [R=301,L]
# Specify search friendly URLs
RewriteRule ^about-me$ /aboutme.php [L]
RewriteRule ^portfolio$ /portfolio.php [L]
RewriteRule ^motion$ /motion.php [L]
RewriteRule ^contact$ /contact.php [L]
RewriteRule ^contact-thanks$ /contact_thanks.php [L]
</IfModule>
You have to make it in the other direction.
If you want to rewrite aboutme.php to about-me
you should use
RewriteRule ^aboutme.php /about-me [R=301,L]
EDIT:
The place /about-me has to be present. If not, you can redirect this virtual place to a file (say rewrite.php) which gets the actual file from database or some configuration. Place the following rules at the bottom of your htaccess rules.
#don't rewrite the rewrite script
RewriteRule rewrite.php - [L]
#as example don't rewrite the image folder
RewriteRule ^images - [L]
#everything else that doesn't match a rule
RewriteRule ^(.*)$ rewrite.php?file=$1 [QSA,L]
after that you can access your requested virtual file in rewrite.php with $_GET['file'].
EDIT:
your rewrite.php could look like this: (this is the simplest way todo. but you have to do some error checking etc.)
<?php
$file = $_GET['file'];
$config = array(
'about-me' => 'aboutme.php'
);
include($config[$file]);
?>

htaccess similar file names goes to same page

Most of the RewriteRules are working fine but there are a couple that have the same word in them and aren't going to the correct page.
Here is my full htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
# Rewrites for main pages
RewriteRule home index.php
RewriteRule about-us about-us.php
RewriteRule business-advice business-advice.php
RewriteRule associates associates.php
RewriteRule become-an-associate associates-sign-up.php
RewriteRule blog blog.php
RewriteRule contact-us contact-us.php
RewriteRule log-in log-in.php
RewriteRule sign-up sign-up.php
The problem resides within the two associated links. When I go to [MYURL]/associates it works fine but if I go to [MYURL]/become-an-associate it takes me to the correct URL but shows content from [MYURL]/associates
Anybody got any ideas?
Thanks,
The pattern for RewriteRule's are regular expressions and the rules all loop until the URI stops changing. That means the first time around, when you request /become-an-associate, it matches and is rewritten to /associates-sign-up.php. Then, the second time around, the rule with the pattern associates matches because of "/associates-sign-up.php". You need to add boundary checks (e.g. ^ and $) as well as the [L] flag:
# Rewrites for main pages
RewriteRule ^home/?$ index.php [L]
RewriteRule ^about-us/?$ about-us.php [L]
RewriteRule ^business-advice/?$ business-advice.php [L]
RewriteRule ^associates/?$ associates.php [L]
RewriteRule ^become-an-associate/?$ associates-sign-up.php [L]
RewriteRule ^blog/?$ blog.php [L]
RewriteRule ^contact-us/?$ contact-us.php [L]
RewriteRule ^log-in/?$ log-in.php [L]
RewriteRule ^sign-up/?$ sign-up.php [L]

Resources