Forum Moderators: phranque

Message Too Old, No Replies

htaccess rewritecond, rewriterule

help with rewritecond, rewriterule

         

almond123

12:27 pm on Jan 22, 2010 (gmt 0)

10+ Year Member



Hi,

i'm a noob in htaccess coding, so could you help me

i am trying to add a slash at the end of a given url.

how can i redirect url for example

[site.example.com...] to [site.example.com...]

also cms is a directory, so i need a rewrite rule, which indicates that if ther's no other characters it will redirect.

http://site.example.com/cms - Redirect
http://site.example.com/cms/asd - Don't redirect

my htaccess code is :

# Turn on URL rewriting
RewriteEngine On

RewriteBase /

# Protect application and system files from being viewed
RewriteRule ^(libs¦modules) - [F,L]

#here should be redirect
RewriteCond %{REQUEST_FILENAME} /cms$ [NC]
RewriteRule ^(.*)$ http://site.example.com/cms/ [R,L]
#redirect end

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
# Rewrite all other URLs to index.php/URL
RewriteRule .* /index.php?cms=1&url=$0 [PT,L]

Thanks

[edited by: jdMorgan at 2:37 pm (utc) on Jan. 22, 2010]
[edit reason] example.com [/edit]

jdMorgan

2:36 pm on Jan 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You were checking REQUEST_FILENAME instead of REQUEST_URI, and Apache may have already (internally) added a trailing slash to this path. There were also several other inefficiencies --large and small-- in the code, all of which are corrected below. Notably, the last rule will now run at least 50% faster, and you may even notice this speed increase from the browser view.

# Turn on URL rewriting
RewriteEngine On
#
# Protect application and system files from being viewed
RewriteRule ^(libs¦modules) - [F]
#
# Externally redirect to add trailing slash to requests for "/cms"
RewriteRule ^cms$ http://site.example.com/cms/ [R=301,L]
#
# If not a request for index.php
RewriteCond $1 !^index\.php$
# RewriteCond $1 !(\.(gif¦jpe?g¦css¦js¦ico)¦^robots\.txt¦sitemap\.xml)$
# and if requested URL does not resolve to existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Internally rewrite to index.php/cms=1&url=<requested-URL-path>
RewriteRule ^(.*)$ /index.php?cms=1&url=$1 [PT,L]

Important: Replace the broken pipe "¦" characters with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Consider adding further exclusions to the final rule for robots.txt, sitemap.xml, any requested URL-path that ends with .gif, .jpg, .jpeg, .css, .js, or .ico, and any other frequently-requested objects which you *know* must (or should) exist on your server. Avoiding unnecessary file- and directory-exists checks will result in server speed improvements and less wear-and-tear on your hard drive. I show an example RewriteCond (currently commented-out) to exclude some of these objects.

Also consider adding canonicalization rules to redirect direct client requests for /index.php to "/", and to redirect non-canonical hostname requests to the canonical hostname (e.g. redirect non-www to www). If added, these rule should go after your trailing-slash redirect.

Jim

almond123

3:35 pm on Jan 22, 2010 (gmt 0)

10+ Year Member



thanx for your answer, but it doesn't help,

RewriteRule ^cms$ [site.example.com...] [R=301,L] works great, but it doesn't redirect when ther's
RewriteRule ^(.*)$ /index.php?cms=1&url=$1 [PT,L]

it sort of doesn't react to this line

<snip>

[edited by: jdMorgan at 5:33 pm (utc) on Jan. 22, 2010]
[edit reason] No URLS, please. See Terms of Service and Charter. [/edit]

jdMorgan

5:37 pm on Jan 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm sorry, but I don't understand what you are saying above.

Please state what URL-paths you tested with, what results you expected, and how those results differed from what you expected. Include any relevant lines from your server error log.

Jim

g1smd

12:17 am on Jan 23, 2010 (gmt 0)

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



The second line (the one with [PT] in it) isn't a redirect, so I also don't understand what you mean.