Forum Moderators: phranque
DirectoryIndex index.html index.php
Options +FollowSymLinks
RewriteEngine On
#Redirect .com/index.html .php .htm to .com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /example.com/index\.(html?|php)\ HTTP/
RewriteRule ^index\.(html?|php)$ /example.com/ [R=301,L]
# Externally redirect to canonicalize the domain name if a non-canonical
# hostname is requested, in order to prevent duplicate-content problems
#RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
#RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#Rewrite .com/12-category-name/index.html to index.php?category_id=12&category_name=category-name
RewriteRule ^([0-9]+)-([^/.]+)/index\.html$ /example.com/index.php?category_id=$1&category_name=$2 [L]
DirectoryIndex index.html index.php
Options +FollowSymLinks
RewriteEngine On
#Redirect .com/index.html .php .htm to .com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.(html?|php)\ HTTP/
RewriteRule ^index\.(html?|php)$ http://www.example.com/ [R=301,L]
# Externally redirect to canonicalize the domain name if a non-canonical
# hostname is requested, in order to prevent duplicate-content problems
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#Rewrite .com/12-category-name/index.html to index.php?category_id=12&category_name=category-name
RewriteRule ^([0-9]+)-([^/.]+)/index\.html$ http://www.example.com/index.php?category_id=$1&category_name=$2 [L]
Your third rule is a 302 redirect. Remove the protocol and hostname to change it to a rewrite.
I duplicate the rules and comment out the set that do not apply, just before uploading.
#Rewrite .com/12-category-name/index.html to index.php?category_id=12&category_name=category-name
RewriteRule ^([0-9]+)-([^/.]+)/index\.html$ /index.php?category_id=$1&category_name=$2 [L] I am creating it and will post here, so can you please verify if it work or not?
If just to put flag then sorry to ask that by default each redirect is 302 redirect?
DirectoryIndex index.html index.php
Options +FollowSymLinks
RewriteEngine On
#Redirect .com/index.html .php .htm to .com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ ([/[A-Za-z0-9-]+]*)?/index\.(html?|php)\ HTTP/
RewriteRule ^([a-z0-9-/]*)index\.(html?|php)$ %1/ [R=301,L]
# Externally redirect to canonicalize the domain name if a non-canonical
# hostname is requested, in order to prevent duplicate-content problems
# 1st RewriteCond to check for dot because actual domain always have dot in it and localhost server does not
RewriteCond %{HTTP_HOST} ([\.]+)
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#Rewrite .com/12-category-name/index.html to index.php?category_id=12&category_name=category-name
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (/[A-Za-z0-9-]+)?/([0-9]+)-([^/.]+)/index\.html\ HTTP/
RewriteRule ^([0-9]+)-([^/.]+)/index\.html$ %1/index.php?category_id=$1&category_name=$2 [L]
# 1st RewriteCond to check for dot because actual domain always have dot in it and localhost server does not
RewriteCond %{HTTP_HOST} ([\.]+)
#Rewrite .com/12-category-name/index.html to index.php?category_id=12&category_name=category-name
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (/[A-Za-z0-9-]+)?/([0-9]+)-([^/.]+)/index\.html\ HTTP/
RewriteRule ^([0-9]+)-([^/.]+)/index\.html$ %1/index.php?category_id=$1&category_name=$2 [L]
As written, the first set-- the optional one-- is the only one that might be used in the Rule. But this part of the request can never occur, because the Rule itself has an opening anchor. So either get rid of the anchor or put the (/[A-Za-z0-9-]+)? part into the Rule itself.
Does the name of this optional directory always start with a letter? If so, say
([A-Za-z][A-Za-z0-9-]+/)?
That way, the RewriteRule will not have to backtrack if the URL only has the second part-- the part that has only numbers before the hyphen.
Your target now becomes
$1index.php?category_id=$2&category_name=$3
The back-reference numbers are the same even if one capture is empty.
Come to think of it: What is the index.html doing there at all? This is a brand-new site, right? So you don't have to deal with outdated links or bookmarks. There's no reason for "index.html" to be there in the first place.
DirectoryIndex index.html index.php
Options +FollowSymLinks
RewriteEngine On
#Redirect .com/[anything-or-not/]index.html .php .htm to .com/[anything-or-not/]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ ([/[A-Za-z0-9-]+]*)?/index\.php\ HTTP/
RewriteRule ^[a-z0-9-/]*index\.php$ %1/ [R=301,L]
# Externally redirect to canonicalize the domain name if a non-canonical
# hostname is requested, in order to prevent duplicate-content problems
# 1st RewriteCond to check for dot because actual domain always have dot in it and localhost server does not
RewriteCond %{HTTP_HOST} \.
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#Rewrite .com/our/contact/ to index.php?type=1&id_name=contact
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([A-Za-z0-9-]+/)?our/[a-zA-Z0-9-]+/\ HTTP/
RewriteRule ^our/([a-zA-Z0-9-]+)/$ /%1index.php?type=1&id_name=$1 [L]
RewriteCond %{HTTP_HOST} \.
RewriteRule ^abc1$ abc.php [L]
RewriteRule ^abc2$ abc.html [L]
RewriteRule ^abc3$ abc.htm [L]
# Rules that block access to malicious requests; i.e. end [F]
# Rules that redirect to new URL; i.e. end [R=301,L]
## Rules that redirect specific requests
## Rules that redirect index requests
## Rules that redirect non-www/www
# Rules that internally rewrite; i.e. end [L]
I have total of 3 confusions right now:
can't understand why to change (/[A-Za-z0-9-]+)?/ with ([A-Za-z0-9-]+/)?
One terminology you used above is opening anchor, basically I dont know what you mean by anchor so it is problem in understanding this point.
No, it may be possible that it starts with numbers as well. But you say something here about backtrack, can you please explain it if you wish so?
why you change that %1 to $1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ ([/[A-Za-z0-9-]+]*)?/index\.php\ HTTP/
RewriteRule ^[a-z0-9-/]*index\.php$ %1/ [R=301,L]