Forum Moderators: phranque

Message Too Old, No Replies

htaccess help directory don't work redirect

         

grigione

5:35 pm on Apr 16, 2011 (gmt 0)

10+ Year Member



i want redirect my www.site.com to h**p://mysite.com
i have used following code:
RewriteEngine On
RewriteCond %{http_host} ^www.mysite.com
RewriteRule ^(.*) h**p://mysite.com/$1 [R=301,L] copied in htaccess file in root of my website

this code work but in my site i have a folder called "directory" where i have installed a directory
in this folder is present an other htacccess file used only for directory
the code of this htaccess is following:
Options -Indexes
Options +FollowSymLinks

DirectoryIndex index.php

<Files ~ "^(.*)\.(tpl|txt)$">
Order deny,allow
Deny from all
</Files>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /directory

RewriteRule ^[/]?latest-links.html$ index.php?l=l [NC]
RewriteRule ^[/]?premium-links.html$ index.php?pm=l [NC]
RewriteRule ^[/]?top-hits.html$ index.php?list=top [NC]
RewriteRule ^[/]?contactus.html$ contactus.php [NC]
RewriteRule ^[/]?linkus.html$ linkus.php [NC]
RewriteRule ^[/]?detail/link[-]?([0-9]*)\.htm[l]?$ detail.php?id=$1 [NC]
RewriteRule ^[/]?info/([0-9]*).*[\.]?[htm]?[l]?$ info.php?br=$1
RewriteRule ^(.*)/page[-]?([0-9]*)\.htm[l]?$ index.php?uri=$1&p=$2 [QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?uri=$1 [QSA,L]
</IfModule>


the problem is that if i type in browser http;//wwww.mysite.com it redirect to h**p://mysite.com
but if i type h**p://wwww.mysite.com/director it don't redirect to h**p://mysite.com/directory and this happen also for all subfolder of directory folder

so the redirect working only in root of my site
How to modify htaccess to make working also for my directory?
thanks

g1smd

6:44 pm on Apr 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There are a huge number of syntax errors in the code and it is grossly inefficient in operation. Many of the rules allow, and actively promote, Duplicate Content issues.

For "page" URLs always use a hyphen. Don't make it optional. The new code corrects requests where it is missing.

In the root of your site place something like this:

<Files ~ "\.(tpl|txt)$">
Order deny,allow
Deny from all
</Files>

# DirectoryIndex and RewriteEngine
DirectoryIndex index.php index.html
RewriteEngine On
Options -Indexes +FollowSymLinks

# Redirect .htm requests to .html and force non-www at the same time
# (force addition of hyphen in two "paged" rules; hyphen is no longer optional)
RewriteRule ^(directory/((latest|premium)-links|top-hits|(contact|link)us))\.htm$ http://example.com/$1.html [R=301,L]
RewriteRule ^(directory/detail/link)-?([0-9]+)\.htm$ http://example.com/$1-$2.html [R=301,L]
RewriteRule ^(directory/info/[0-9]+[^/.]+)\.htm$ http://example.com/$1.html [R=301,L]
RewriteRule ^(directory/[^/]+/page)-?([0-9]+)\.htm$ http://example.com/$1-$2.html [R=301,L]

# Redirect to force hyphens in paged .html requests (hyphen no longer optional)
RewriteRule ^(directory/detail/link)([0-9]+)\.html$ http://example.com/$1-$2.html [R=301,L]
RewriteRule ^(directory/[^/]+/page)([0-9]+)\.html$ http://example.com/$1-$2.html [R=301,L]

# Site-wide www to non-www canonicalisation
RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) http://example.com/$1 [R=301,L]

# RewriteBase
RewriteBase /directory

# Internal rewrites for valid .html requests. For "paged" URLs hyphen is required.
RewriteRule ^directory/latest-links\.html$ index.php?l=l [L]
RewriteRule ^directory/premium-links\.html$ index.php?pm=l [L]
RewriteRule ^directory/top-hits\.html$ index.php?list=top [L]
RewriteRule ^directory/(contact|link)us\.html$ $1us.php [L]
RewriteRule ^directory/detail/link-([0-9]+)\.html$ detail.php?id=$1 [L]
# (also need to modify PHP script to check $slug variable
# is valid for requested $br value in next rule)
RewriteRule ^directory/info/([0-9]+)([^/.]+)\.html$ info.php?br=$1&slug=$2 [L]
RewriteRule ^directory/([^/]+)/page-([0-9]+)\.html$ index.php?uri=$1&p=$2 [QSA,L]

# Internal rewrite
RewriteCond %{REQUEST_URI} --need pattern here to pre-check request so that only URLs that will be rewritten need to be "-f" and "-d" checked for clashes--
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^directory/([^/]+/)+$ index.php?uri=$1 [QSA,L]
# The above line is a guess. You might need this instead:
RewriteRule ^directory/([^/]+)/$ index.php?uri=$1 [QSA,L]
# However if the above URLs are for "pages" then the extensionless
# URL should not end with a slash at all.


The above code may need some minor edits to make everything work, but should be very close to what is required to make the site function correctly and without throwing all your SEO efforts out of the window.

The code you had before allowed there to be "optional" things in the URL. Never allow this; redirect the other variants to the canonical URL (e.g. the rules above force hyphens in paged URLs and redirect .htm requests to .html).

Don't allow unchecked wildcard characters within a URL. In the example above, modify the PHP script so that the $slug value is checked for validity.

The [NC] flag allows aNy Case and will promote Duplicate Content when used with a rewrite. It allows any URL variant to be valid. You should have just one variant as valid.

The new code fixes a large number of issues. You'll need to ensure your pages link to the right version of the URL. Certain things that you previously allowed to be optional must now be included as standard.

grigione

1:40 pm on Apr 17, 2011 (gmt 0)

10+ Year Member



ok

thank you very much


the code working very well after that i have apported some modify:

i have commented this:
#RewriteCond %{REQUEST_URI} --need pattern here to pre-check request so that only URLs that will be rewritten need to be "-f" and "-d" checked for clashes--

because with this rule don't working anything


and modified
RewriteRule ^directory/([^/]+)/$ index.php?uri=$1 [QSA,L]

with

RewriteRule ^directory/(.*)/$ index.php?uri=$1 [QSA,L]


because navigation of subfolder in folder directory don't work without asterisk modify

the new code:


<Files ~ "\.(tpl|txt)$">
Order deny,allow
Deny from all
</Files>

# DirectoryIndex and RewriteEngine

DirectoryIndex index.php index.html

<IfModule mod_rewrite.c>
RewriteEngine on
Options -Indexes +FollowSymLinks

# Redirect .htm requests to .html and force non-www at the same time
# (force addition of hyphen in two "paged" rules; hyphen is no longer optional)

RewriteRule ^(directory/((latest|premium)-links|top-hits|(contact|link)us))\.htm$ http://example.com/$1.html [R=301,L]
RewriteRule ^(directory/detail/link)-?([0-9]+)\.htm$ http://example.com/$1-$2.html [R=301,L]
RewriteRule ^(directory/info/[0-9]+[^/.]+)\.htm$ http://example.com/$1.html [R=301,L]
RewriteRule ^(directory/[^/]+/page)-?([0-9]+)\.htm$ http://example.com/$1-$2.html [R=301,L]

# Redirect to force hyphens in paged .html requests (hyphen no longer optional)

RewriteRule ^(directory/detail/link)([0-9]+)\.html$ http://example.com/$1-$2.html [R=301,L]
RewriteRule ^(directory/[^/]+/page)([0-9]+)\.html$ http://example.com/$1-$2.html [R=301,L]

# Site-wide www to non-www canonicalisation

RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) http://example.com/$1 [R=301,L]

# RewriteBase

RewriteBase /directory

# Internal rewrites for valid .html requests. For "paged" URLs hyphen is required.

RewriteRule ^directory/latest-links\.html$ index.php?l=l [L]
RewriteRule ^directory/premium-links\.html$ index.php?pm=l [L]
RewriteRule ^directory/top-hits\.html$ index.php?list=top [L]
RewriteRule ^directory/(contact|link)us\.html$ $1us.php [L]
RewriteRule ^directory/detail/link-([0-9]+)\.html$ detail.php?id=$1 [L]

# (also need to modify PHP script to check $slug variable
# is valid for requested $br value in next rule)

RewriteRule ^directory/info/([0-9]+)([^/.]+)\.html$ info.php?br=$1&slug=$2 [L]
RewriteRule ^directory/([^/]+)/page-([0-9]+)\.html$ index.php?uri=$1&p=$2 [QSA,L]


# Internal rewrite
#RewriteCond %{REQUEST_URI} --need pattern here to pre-check request so that only URLs that will be rewritten need to be "-f" and "-d" checked for clashes--

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#RewriteRule ^directory/([^/]+/)+$ index.php?uri=$1 [QSA,L]
# The above line is a guess. You might need this instead:

RewriteRule ^directory/(.*)/$ index.php?uri=$1 [QSA,L]

# However if the above URLs are for "pages" then the extensionless
# URL should not end with a slash at all.

</IfModule>

g1smd

5:31 pm on Apr 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Remove these two lines:
<IfModule mod_rewrite.c>
</IfModule>

You do not want mod_rewrite to silently fail. You need to know about it and fast.


YOU will need to write the pattern for the "pattern goes here" line. That's what the "pattern goes here" line means. You absolutely do not want every URL request to invoke two slow "exists" checks on the server hard drive. The pattern here will accept only URL formats that might be rewritten or will reject all formats that will never be rewritten. Only you can know what that pattern can be.


This line:
RewriteRule ^directory/(.*)/$ index.php?uri=$1 [QSA,L]

should not use the (.*) pattern. It is very inefficient.
You should consider other options. I would use:
RewriteRule ^directory/([^/]+/)+$ index.php?uri=$1 [QSA,L]

and make sure the final "/" is stripped from the $uri variable within the PHP script.
Alternatively, as the comment says, move to extensionless URLs without a trailing slash:
RewriteRule ^directory/([^/]+/)*[^/.]+)$ index.php?uri=$1 [QSA,L]