Forum Moderators: phranque
<a href="/id_number/hyphenated_article_title/">Anchor_text</a> <a href="/12/how-to-lose-weight-fast/">How to Lose Weight Fast</a> RewriteEngine on
RewriteRule ^/([0-9]+)/[A-Za-z0-9-]+/?$ article.php?id=$1 [NC,L] www.example.com/12/how-to-lose-weight-fast/ www.example.com/12/how-to-lose-weight-fast/contact.html RewriteEngine on
RewriteRule ^/
The rewrite is working, but article.php is loading slowly and without CSS styling.
Every link on my website now begins with this:
www.example.com/
www.example.com/12/how-to-lose-weight-fast/ www.example.com/12-how-to-lose-weight-fast www.example.com/7362-a-page-about-stuff www.example.com/12-how-to- www.example.com/12-this-product-is-rubbish www.example.com/12/how-to-lose-weight-fast,,,,,.......------- RewriteRule ^/([0-9]+)/[A-Za-z0-9-]+/?$ article.php?id=$1 [NC,L] example.com/article.php?id=12 or similar URL? Have those types of URL ever been live on the web? If they have, you need a few more lines of RewriteRule code to redirect those requests to the new format. www.example.com/12/how-to-lose-weight-fast/ style URLs ever live on the web? A simple RewriteRule can redirect all of those requests to the new URL format.
I assume you already have a non-www/www canonical redirect as the last of your external redirects and before the internal rewrites.
# Always use www in the domain
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1example.com%{REQUEST_URI} [R=301,L]
RewriteRule ^([0-9]+)-([A-Za-z0-9-]+)/?$ article.php?id=$1&slug=$2 [L]
RewriteRule ^videos/health/([0-9]+)/?$ videos.php?cat=1&page=$1 [L]
ErrorDocument 404 /404.php What happens if someone requests example.com/article.php?id=12 or similar URL? Have those types of URL ever been live on the web?
RewriteRule On add two new rules: RewriteRule ^([0-9]+)-([A-Za-z0-9-]+)/$ http://www.example.com/$1-$2 [R=301,L] RewriteRule ^videos/health/([0-9]+)/$ http://www.example.com/videos/health/$1 [R=301,L] RewriteRule ^([0-9]+)-([A-Za-z0-9-]+)$ /article.php?id=$1&slug=$2 [L] RewriteRule ^videos/health/([0-9]+)$ /videos.php?cat=1&page=$1 [L] RewriteRule. Makes the code more readable. I assume you already have a non-www/www canonical redirect as the last of your external redirects and before the internal rewrites.
That's way over my head! I don't even understand what you mean!
# Always use www in the domain
^(www\.example\.com)?$
RewriteRule ^([0-9]+)-([a-z0-9-]+)/$ http://www.example.com/$1-$2 [R=301,L] www.example.com/123-hyphenated-lowercase-article-title RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1example.com%{REQUEST_URI} [R=301,L]
RewriteRule ^([0-9]+)-([a-z0-9-]+)$ /article.php?id=$1&slug=$2 [L]
RewriteRule ^browse/([0-9]+)$ /browse.php?page=$1 [L]
RewriteRule ^videos/health/([0-9]+)$ /videocategory.php?cat=1&page=$1 [L]
RewriteRule ^videos/fitness/([0-9]+)$ /videocategory.php?cat=2&page=$1 [L]
RewriteRule ^videos/weight-loss/([0-9]+)$ /videocategory.php?cat=3&page=$1 [L]
RewriteRule ^videos/self-esteem/([0-9]+)$ /videocategory.php?cat=4&page=$1 [L]
RewriteRule ^about$ /about-us.php [L]
RewriteRule ^contact$ /contact-us.php [L]
RewriteRule ^privacy$ /privacy-policy.php [L]
RewriteRule ^terms$ /terms-of-use.php [L]
ErrorDocument 404 /404.php
RewriteRule ^([0-9]+)-([A-Za-z0-9-]+)$ /article.php?id=$1&slug=$2 [L] Is the slash meant to be there? My URLs don't have a slash at the end.
but doesn't my very first rewrite rule already make all links canonical
The problem is that, if someone then uses an upper case letter, they will get a 404 error.
If I keep A-Z, they will not get a 404 error because my PHP code redirects to the right, lowercase URL.
Below is my complete .htaccess file.
RewriteEngine On # Change first slash to hyphen and strip optional trailing slash if requested.
# Redirect those requests to canonical hostname at the same time.
RewriteRule ^([0-9]+)/([A-Za-z0-9-]+)/?$ http://www.example.com/$1-$2 [R=301,L]
# Strip trailing slash and redirect those requests to canonical hostname at the same time.
RewriteRule ^([0-9]+)-([A-Za-z0-9-]+)/$ http://www.example.com/$1-$2 [R=301,L]
RewriteRule ^browse/([0-9]+)/$ http://www.example.com/browse/$1 [R=301,L]
RewriteRule ^videos/(health|fitness|weight-loss|self-esteem)/([0-9]+)/$ http://www.example.com/videos/$1/$2 [R=301,L]
RewriteRule ^(about|contact|privacy|terms)/$ http://www.example.com/$1 [R=301,L]
# Non-www to www hostname canonicalisation redirect. it's in the wrong place
That was probably a typo on g1's part.
Don't you also have a bunch of rules that redirect requests from old ugly URL to new pretty URL?
<?php
$id = $_GET['id'];
$slug = $_GET['slug'];
$conn = mysql_connect("", "", "") or die(mysql_error());
mysql_select_db("");
$res = mysql_query("SELECT id, slug FROM `table` WHERE id = $id", $conn) or die(mysql_error());
$row = mysql_fetch_assoc($res);
if($row['id']) {
if($slug !== $row['slug']) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.example.com/$id-{$row['slug']}");
die();
}
else {
$sql = "SELECT article FROM `table` WHERE id = $id";
$results = mysql_query($sql, $conn) or die(mysql_error());
$row = mysql_fetch_assoc($results);
}
} // End first if
else {
header("HTTP/1.1 404 Not Found");
include('http://www.example.com/404.php');
die();
}
?> RewriteRule ^(contact-us)\.php$ http://www.example.com/$1 [R=301,L] RewriteRule ^contact-us$ /contact-us.php [L] RewriteCond %{THE_REQUEST} ^[A-Z]+\ /ugly\.html\ HTTP/
# At top of file, in the external redirects section:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /contact-us\.php\ HTTP/
RewriteRule ^(contact-us)\.php$ http://www.example.com/$1 [R=301,L]
# At bottom of file, in the internal rewrites section:
RewriteRule ^contact-us$ /contact-us.php [L]
<?php
$id = $_GET['id'];
$slug = $_GET['slug'];
$conn = mysql_connect("", "", "") or die(mysql_error());
mysql_select_db("");
$res = mysql_query("SELECT id, slug FROM `table` WHERE id = $id", $conn) or die(mysql_error());
$row = mysql_fetch_assoc($res);
if(!$row['id']) {
header("HTTP/1.1 404 Not Found");
include('404.php');
die();
}
else {
if($slug !== $row['slug']) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: $id-{$row['slug']}");
die();
}
else {
$sql = "SELECT article FROM `table` WHERE id = $id";
$results = mysql_query($sql, $conn) or die(mysql_error());
$row = mysql_fetch_assoc($results);
}
}
?>
RewriteCond %{QUERY_STRING} ^catid=([0-9]+)$
RewriteRule ^category\.php$ http://www.example.com/%1? [R=301,L] RewriteCond %{QUERY_STRING} ^page=[0-9]+$
RewriteRule ^index\.php? http://www.example.com? [R=301,L] www.example.com/images/ Another thing that I sometimes do is have the 404 or the 301 process write to a custom log file recording the date and time, requested URL, user agent, referrer and other things.
^[A-Z]{3,9}\ ^GET\
RewriteRule ^index\.php? http://www.example.com? [R=301,L]
example.com/?page=123 ^(index\.php)? We match it up to here:
HTTP/
Never here
^[A-Z]{3,9}\ \S+\s If it's always just "GET", why not use this:
^GET\
[edited by: engine at 8:59 am (utc) on Sep 28, 2013]
[edit reason] fixed typo [/edit]
RewriteCond %{QUERY_STRING} ^catid=([0-9]+)$ RewriteCond %{QUERY_STRING} ^page=[0-9]+$ RewriteCond %{QUERY_STRING} (^|&)catid=([0-9]+)(&|$) RewriteCond %{QUERY_STRING} (^|&)page=[0-9]+(&|$) ^index\.php? matches index.php and index.ph ^(index\.php)?$ and that "$" on the end is very important. http://www.example.com? http://www.example.com/? [edited by: g1smd at 9:30 am (utc) on Sep 28, 2013]
Note that I made a typo
when I included the question mark in the rewrite pattern (i.e., ^index.php?). It was supposed to be a dollar sign.
"RewriteBase /" is used at the top of the file to avoid having to include "^/" before each rewrite rule pattern.
This directive is required when you use a relative path in a substitution
Shall I adjust the pagination script such that, when there are no results, a 404 is given?
msg:4612861 but didn't see a specific acknowledgement that you had fixed that. It's important. Add it to the list of stuff to tick off. :)