Forum Moderators: phranque

Message Too Old, No Replies

problems with extensionless URLs

Problems when moving mod_rewrite into PROD env

         

cesmendez

8:45 pm on Oct 25, 2007 (gmt 0)

10+ Year Member



I have been following many posts by jdMorgan about how to achieve extensionless URLs. In doing so I have accomplished this so thank you jdMorgan. Unfortunately when I move my code into production it doesn't work. The code looks like this:

RewriteEngine on
#
## internally rewrite extensionless file requests to .html files ##
#
# If the requested URI does not contain a period in the final path-part
RewriteCond %{REQUEST_URI}!(\.[^./]+)$
# then add .html to get the actual filename
RewriteRule (.*) /$1.html [L]

I placed this in the httpd.conf file. Like I said when i go to http://www.example.com/test it resolves to http://www.example.com/test.html which is what I want but when I placed it into PROD it doesn't work. I am trying to do this for only one file in the entire site, which is really big. I am a newbie to this and I am noticing a lot of rewrites and ifmodules all over the file.
I know that there is a difference in the way that it should be written if it is in the conf file or in a .htaccess file, which I tried by the way and didn't work. Any way that you suggest would be greatly appreciated.

Thanx
Cesar

jdMorgan

9:47 pm on Oct 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For use in httpd.conf or conf.d:

# If the requested URI does not contain a period in the final path-part
RewriteCond %{REQUEST_URI} !\.[^./]+$
# then add .html to get the actual filename
RewriteRule (.*) $1.html [L]

-or-

# If the requested URI does not contain a period in the final path-part
RewriteCond %{REQUEST_URI} !\.[^./]+$
# then add .html to get the actual filename
RewriteRule ^/(.*) /$1.html [L]

In addition, this code must be located in the proper <Directory> or <Location> container inside the proper <VirtualHost> (or server) container. You must also have the FollowSymLinks or SymLinksIfOwnerMatch Option enabled in that container.

Jim

cesmendez

10:04 pm on Oct 25, 2007 (gmt 0)

10+ Year Member



Thanx Jim.

In a .htaccess file should it simply be placed in the directory in which the change should occur? Should I go with the change in the httpd.conf file or with a .htaccess file?

BTW thanx for the quick reply.

jdMorgan

1:24 pm on Oct 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can place an .htaccess file in the directory where it is to be applied, or in any Web-accessible directory above that directory. The URL-path 'seen' by RewriteRule will change depending on the directory level where the RewriteRule resides; It will be 'localized' to the current directory level, so the RewriteRule pattern will need to be adjusted to account for this. In simple terms, the path to the current directory is stripped off of the URL-path examined by RewriteRule.

However, this is a minor adjustment, so the location decision usually revolves around two issues - Efficiency and maintainability. The code will be more efficient (executed less often) if located in the directory where it is to be applied, or 'closer' to the directory where it is to be applied. On the other hand, the code will be easier to maintain --and the possibilities for unexpected code interactions less likely to go unnoticed-- if the code is 'centralized' in the top-level .htaccess file.

The choice between putting the code in a server config file such as httpd.conf or conf.d versus putting it in .htaccess files also boils down to two primary issues - Efficiency and ease of testing. Code in .htaccess is interpreted for each HTTP request. That is, it is parsed as plain-text and converted to an executable equivalent for every HTTP request that involves the directory in which it resides. This is inefficient, but does allow .htaccess code to be changed at any time --even on-the-fly-- and take immediate effect, without requiring a server restart.

On the other hand, code located in the server config files such as httpd.conf or conf.d is compiled at server restart. That is, it is parsed as plain-text and converted into native CPU instructions once at server restart, and this code image is subsequently used while processing HTTP requests; the original text file is not used again until the server is restarted. So in contrast to code in .htaccess, the same code in a server config file executes *much* faster and more efficiently, but requires a server restart before any changes to that code will take effect.

As a result, many Webmasters opt to develop and test their code in a .htaccess context, and then 'port' that code over to the server config level after it has been tested and debugged.

Note that the majority of Webmasters don't have a choice; Since most of them are on shared name-based servers, they do not have access to the server config files, so .htaccess is the only choice. These Webmasters cannot make use of some of the powerful features available in httpd.conf, such as the ability to define RewriteMaps or to declare custom log file formats (just to name two).

Jim

cesmendez

5:58 pm on Oct 26, 2007 (gmt 0)

10+ Year Member



Thanx but I still can't get it to work. I placed the .htaccess file with the following script
RewriteEngine on
#
## internally rewrite extensionless file requests to .html files ##
#
# If the requested URI does not contain a period in the final path-part
RewriteCond %{REQUEST_URI}!(\.[^./]+)$
# then add .html to get the actual filename
RewriteRule (.*) /$1.html [L]

in the docroot dir and the dir that has the file and still nothing.

cesmendez

11:24 pm on Nov 14, 2007 (gmt 0)

10+ Year Member



Jim-

Unfortunately I could not get this to work. I tried a work around that works, to a certain extent. My alternate solution is that I created another directory called 'library'. In it I placed the file that I wanted to get to which I called index.html. This works when I type out the address using the server name. As soon as I use the actual site name [site.com...] it doesn't work. One thing that I noticed in the httpd.conf file was that the virtual host was set up like this:
<VirtualHost www.site.com:80>
ServerName www.site.com
## Include security.conf

RewriteEngine On
RewriteLogLevel 1
RewriteLog logs/rewite.log

## Rewrite Setup for istore. Need to include url_fw_istore_http.conf
RewriteRule ^/(.*) [server.comp.com:8090...]
ProxyRequests Off
ProxyPreserveHost On
<IfModule mod_proxy.c>
ProxyPass / [server.comp.com:8090...]
ProxyPassReverse / [server.comp.com:8090...]
ProxyPass /OA_HTML/ [server.comp.com:8090...]
ProxyPassReverse /OA_HTML/ [server.comp.com:8090...]
ProxyPass /OA_HTML/dir/ [server.comp.com:8090...]
ProxyPassReverse /OA_HTML/dir/ [server.comp.com:8090...]
ProxyPass /oa_servlets/ [server.comp.com:8090...]
ProxyPassReverse /oa_servlets/ [server.comp.com:8090...]
ProxyPass /servlets/ [server.comp.com:8090...]
ProxyPassReverse /servlets/ [server.comp.com:8090...]
</IfModule>
</VirtualHost>

So when I try to hit server.comp.com:8090/OA_HTML/library it doesn't work. But when I try server.comp.com:8000/OA_HTML/library it resolves perfectly. Any ideas? This is the conf file on the proxy server.