Can somebody check my code please

New to mod_rewrite? This is a good place to start.

Can somebody check my code please

Postby alhermette » Wed Jun 03, 2009 2:42 pm

I have written a simple .htaccess file but as I am new to this I would appreciate it if anyone can point out any errors that I may have made (or confirm that it's OK).

I have a php site but want it to appear as static html no matter whether the page requested os html or php. I have commented as best I can to make it obvious what I am trying to achieve.

Options +FollowSymLinks
RewriteEngine on

# Disable directory browsing
Options All –Indexes

# Prevent folder listing
IndexIgnore *

#Block viewing of .htaccess
RewriteRule \.htaccess - [F]

# Force www
RewriteCond %{HTTP_HOST} ^(domain\.com)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301, L]

# Remove index file
RewriteCond %{THE_REQUEST} \/index.(php|html)\ HTTP
RewriteRule (.*)index.(php|html)$ /$1 [R=301, NC, L]

# Redirect php requests to html
RewriteRule ^(.*)\.php$ $1.html [R=301, NC, L]

# Rewrite html requests to corresponding php file
RewriteRule ^(.*)\.html$ $1.php [NC, L]


Any help much appreciated,

Allan
alhermette
 
Posts: 18
Joined: Mon Mar 16, 2009 2:44 pm

Postby richardk » Thu Jun 04, 2009 1:20 pm

Code: Select all
Options All -Indexes

You can't mix +/- and not +/- options.

Code: Select all
#Block viewing of .htaccess
RewriteRule \.htaccess - [F]

You almost certainly don't need that. Can you view .htaccess files without it?

Code: Select all
[R=301, L]

There should be no spaces in any flags.

Code: Select all
# Redirect php requests to html
RewriteRule ^(.*)\.php$ $1.html [R=301, NC, L]

This rule will cause a loop as it will match the internal requests to the PHP file (from the rule below it).

Code: Select all
IndexIgnore *
Options All
Options +FollowSymLinks -Indexes

RewriteEngine On

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

# Remove index file
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.(php|html)(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.(php|html)$ /$1 [NC,R=301,L]

# Redirect php requests to html
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\.php$ /$1.html [NC,R=301,L]

# Rewrite html requests to corresponding php file
RewriteCond %{DOCUMENT_ROOT}/$1php -f
RewriteRule ^(.+\.)html$ /$1php [NC,QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby alhermette » Fri Jun 05, 2009 2:54 am

Thank you for setting me straight, it is most helpful.

I forgot one thing though, I have an application located in one directory that has its own .htaccess file and I therefore do not want to conflict with what this is doing. Am I correct in saying that by default my main .htaccess rules will be ignored by the application as it has its own .htaccess file? If not are both .htaccess files processed?

If not then I suppose the easiest solution would be to put a condition in the main .htaccess file that matches the directory in question and a rule that states no rewriting (I assume that the .htaccess file in the directory would then process the url as intended). How would I do this?
alhermette
 
Posts: 18
Joined: Mon Mar 16, 2009 2:44 pm

Postby richardk » Fri Jun 05, 2009 10:50 am

Am I correct in saying that by default my main .htaccess rules will be ignored by the application as it has its own .htaccess file?

Yes, it should.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby alhermette » Mon Jun 15, 2009 11:14 am

I needed to add a custom 404 error page so I added the following line

ErrorDocument 404 /Common/Errors/404.html


If I type the url of my error page into a browser it rewrites it to the original php page as intended and the page displays fine. If I use a non existant url however all relative links and images from my template are incorrect unless the requested url happened to b in the Common directory.

It would seem that the links are being generated relative to the requested file (which may be non existant) as opposed to the error file that I created. Any ideas as to how to overcome this behaviour and is there a preferred place for entering the ErrorDocument line in the htaccess file?

By the way your code works perfectly, thanks for the help.
alhermette
 
Posts: 18
Joined: Mon Mar 16, 2009 2:44 pm

Postby richardk » Mon Jun 15, 2009 12:00 pm

If I use a non existant url however all relative links and images from my template are incorrect unless the requested url happened to b in the Common directory.

In the HTML output? FAQ: Relative paths to images, JavaScript, CSS and other external/linked files are broken.

and is there a preferred place for entering the ErrorDocument line in the htaccess file?

It doesn't matter.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby alhermette » Wed Jun 17, 2009 12:24 am

Thanks the FAQ link answered my question and it is a problem of the links being relative to what is displayed in the address bar as opposed to the 404 pahe that I am displaying.

I converted my template to a plain html page and edited all the links to be absolute and it all works perfectly now. Pity to have to ditch the template but life never seems to let you do what you want to and at least it works properly now.

Thanks
alhermette
 
Posts: 18
Joined: Mon Mar 16, 2009 2:44 pm


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 100 guests

cron