SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    May 2007
    Location
    West Coast
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    cannot get rewrite to work extension

    I cannot seem to get a rewrite to work after trying about 4-5 different ways.

    Is there a specific area to insert it?

    code that is unnecessary? that Frontpage stuff

    I want it to simply to 2 rewrites.

    remove the file extension ".php" and if someone types just website.com that it changes to www.website.com which already works but I cannot make both work.

    both should be perm 301's

    Code:
    # -FrontPage-
    
    IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
    
    <Limit GET POST>
    order deny,allow
    deny from all
    allow from all
    </Limit>
    <Limit PUT DELETE>
    order deny,allow
    deny from all
    </Limit>
    AuthName website.com
    AuthUserFile /home/main/public_html/_vti_pvt/service.pwd
    AuthGroupFile /home/main/public_html/_vti_pvt/service.grp
    
    
    ErrorDocument 404 http://www.website.com/404
    
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
    
    RewriteEngine on 
    RewriteCond %{HTTP_HOST} ^website.com
    RewriteRule (.*) http://www.website.com/$1 [R=301,L]
    
    
    order allow,deny
    deny from
    94.100.22.210 
    213.238.175.8
    188.143.232.31 
    188.143.232.111
    allow from all
    
    
    <IfModule mod_expires.c>
    
    # Enable expirations
    ExpiresActive On
    
    # Default directive
    ExpiresDefault "access plus 1 month"
    
    # My favicon
    ExpiresByType image/x-icon "access plus 1 year"
    
    # Images
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    
    # CSS
    ExpiresByType text/css "access 1 month"
    
    # Javascript
    ExpiresByType application/javascript "access plus 1 year"
    
    </IfModule>

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,679
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)

    Apache or IIS?

    Hi lukkas!

    Apache or IIS?

    Comments on your .htaccess code:

    Quote Originally Posted by lukkas View Post
    I want it to simply to 2 rewrites.

    remove the file extension ".php" and if someone types just website.com that it changes to www.website.com which already works but I cannot make both work.

    both should be perm 301's

    Code:
    # -FrontPage-
    
    IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
    
    
    That appears to be IIS-specific as your "escape" character is a forward /, not a \. The forward slash may work on a Windows OS but not on 'nix with Apache.
    <Limit GET POST> order deny,allow deny from all allow from all </Limit> <Limit PUT DELETE> order deny,allow deny from all </Limit>
    If anything, this will be in your httpd.conf file so it only slows Apache processing of a request.
    AuthName website.com AuthUserFile /home/main/public_html/_vti_pvt/service.pwd AuthGroupFile /home/main/public_html/_vti_pvt/service.grp
    Did you really want to password protect your website?
    ErrorDocument 404 http://www.website.com/404 RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php RewriteEngine on
    You've already ensured that mod_rewrite is NOT in "Comment Mode" so no need to duplicate that check.
    RewriteCond %{HTTP_HOST} ^website.com RewriteRule (.*) http://www.website.com/$1 [R=301,L]
    Two comments: I like to make my global changes first so I would reorder the RewriteRule sets (rule and cond statements) AND, because ^(.*)$ already exists as %{REQUEST_URI}, I'd change each RewriteRule to RewriteRule . %{REQUEST_URI}.php [L] (note that this requires at least one character so you don't ask for .php as the file) and RewriteRule .? http://www.website.com%{REQUEST_URI} [L] (which does NOT require any character to match - and will ALWAYS return true and perform the redirection if the conditions are met).
    order allow,deny deny from 94.100.22.210 213.238.175.8 188.143.232.31 188.143.232.111 allow from all
    If you have access to the server or vhost file, these deny IPs should be stored there (where they'll only be read once ... at Apache start but, if you don't have that access, this is fine.
    <IfModule mod_expires.c>
    ARGH! Same rant as before because you don't want to run the same test multiple times (for each pass through your mod_rewrite code) for EVERY reqauest! [standard rant #4]
    The definition of insanity is to repeatedly do the same thing expecting a different result. Asking Apache to confirm the existence of ANY module with an <IfModule> ... </IfModule> wrapper is the same thing in the webmaster world. DON'T ACT INSANE! If you don't know whether a module is enabled, run the test ONCE (without the wrapper) then delete it permanently as it is EXTREMELY wasteful of Apache's resources (and should NEVER be allowed on a shared server).
    [/standard rant #4] It's bad enough that you have these in an .htaccess file (rather than the server or vhost configuration file) so don't compound the error by running the IfModule test repeatedly, too!
    # Enable expirations ExpiresActive On # Default directive ExpiresDefault "access plus 1 month" # My favicon ExpiresByType image/x-icon "access plus 1 year" # Images ExpiresByType image/gif "access plus 1 month" ExpiresByType image/png "access plus 1 month" ExpiresByType image/jpg "access plus 1 month" ExpiresByType image/jpeg "access plus 1 month" # CSS ExpiresByType text/css "access 1 month" # Javascript ExpiresByType application/javascript "access plus 1 year" </IfModule>
    Okay, I'm commented out.

    I did consider whether you should also check to see whether the %{REQUEST_FILENAME} is a file
    before making the redirection to %{REQUEST_URI}.php (to prevent looping) but, IMHO, you're not
    likely to be using {filename}.php.php so that would be a repetitive test only to check your file naming
    convention, i.e., another waste of time/resources.

    I don't understand WHY your mod_rewrite code does not work UNLESS you're using IIS and not Apache OR
    your password protection is forbidding website access (you didn't comment on that so I'll assume that's
    correct and you're using IIS which does not perfectly duplicate Apache's mod_rewrite).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Addict
    Join Date
    May 2007
    Location
    West Coast
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't have access to the server and need it there to block some spammer. I know there's other ways but so far it has worked.

    From tech support [ "The sever in which your website is hosted is having LiteSpeed webserver. LiteSpeed Web Server's rewrite engine is fully compatible with Apache mod_rewrite so there is no need to change rewrite directives in LiteSpeed. The rewrite engine is highly optimized and increases speed over Apache's rewrite engine, especially for rewrite directives in a .htaccess file."]

    I had already omitted the 2nd "RewriteEngine on" rule too and experienced no change.


    RewriteRule . %{REQUEST_URI}.php [L]

    (note that this requires at least one character so you don't ask for .php as the file)

    and

    RewriteRule .? http://www.website.com%{REQUEST_URI} [L] - no longer a R=301 ??? I didn't want 2 of the same file indexed. Will this ensures it is not in making two of the same file?
    website.com,website.com/index.php, http://website.com, http://www.website.com, website.com/

    - if Module -->

    I was told to just copy and paste that in so it speeds up the website - browser caching. Google site speed checks. I don't want the same person using resources over & over again. it is recommended

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,679
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Hi lukkas,

    My responses are indented with
    Quote Originally Posted by lukkas View Post
    I don't have access to the server and need it there to block some spammer. I know there's other ways but so far it has worked.

    Without access to the server or vhosts config files, you're stuck with using .htaccess for your blocking (unless you can convince your host to block that IP as a hacker).

    From tech support [ "The sever in which your website is hosted is having LiteSpeed webserver. LiteSpeed Web Server's rewrite engine is fully compatible with Apache mod_rewrite so there is no need to change rewrite directives in LiteSpeed. The rewrite engine is highly optimized and increases speed over Apache's rewrite engine, especially for rewrite directives in a .htaccess file."]

    If they say so ... I'm glad to hear that LiteSpeed is "fully compatible."

    I had already omitted the 2nd "RewriteEngine on" rule too and experienced no change.

    That would have been extremely minor ... just the time to read and parse a few extra bytes.

    RewriteRule . %{REQUEST_URI}.php [L]

    (note that this requires at least one character so you don't ask for .php as the file)

    and

    RewriteRule .? http://www.website.com%{REQUEST_URI} [L] - no longer a R=301 ??? I didn't want 2 of the same file indexed. Will this ensures it is not in making two of the same file?
    website.com,website.com/index.php, http://website.com, http://www.website.com, website.com/

    The first prevents a request for http://www.website.com/.php which should yield a 404 error and redirection to your ErrorDocument.

    The second one's regex will ALWAYS evaluate to TRUE so the redirection from non-www to www'd domain will use the Apache variable {REQUEST_URI} rather than creating a duplicate variable, $1, for the redirection. While a few more bytes of code, it resolves any ambiguity about leading /'s.

    - if Module -->

    I was told to just copy and paste that in so it speeds up the website - browser caching. Google site speed checks. I don't want the same person using resources over & over again. it is recommended

    Ah, the ugly problem of copying code without understanding what it does! ARGH!

    The simple reason that canned apps include <IfModule> blocks is that, if the module is not available, the server will throw an error in the .htaccess and disable your website. YOU, as webmaster, are supposed to know this and test without the <IfModule> wrapper knowing to immediately replace it (actually, to delete the code that it wrapped as it's useless without the module) to re-enable your website. Of course, leaving it in place is for "script kiddies" as the wrapped code is protected (will not disable your website in case the module is not available) and they won't know the difference. If you are a webmaster, please learn the lesson that you MUST know and understand what "copied code" is doing and modify for your own (efficient) use.
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •