Forum Moderators: phranque

Message Too Old, No Replies

Need help with mod rewrite

Need help with mod_rewrite

         

idrees

10:37 am on Aug 4, 2009 (gmt 0)

10+ Year Member



Hello,

I am a PHP developer but I dont have expertise in apache and mod_rewrite.

I am using the following rewrite code for my website this code is placed in .htaccess file.

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
RewriteEngine On

# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /

# we skip all files with .something
# comment the following 3 lines to allow periods in routes
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]

# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f

# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

# big crash from our front web controller
ErrorDocument 500 "<h2>Application error</h2>symfony application failed to start properly"

This code is working perfectly fine on my local machine but not with remote server because server has different port that is 8080.

Can anyone alter this code for me so that it will work with my server machine.

Thanks

jdMorgan

2:27 pm on Aug 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nothing in this code refers to the domain or the port number, so your problem is likely due to a different aspect of the server configuration.

Please describe how you are testing, what URLs you are typing in, what the expected results are, what the actual results are, and how the actual results differ from the expected results. Also include any relevant information from your server error log file.

As stated in our forum charter, we cannot write your code for you, but we can help *you* get *your* code working. This code needs several improvements, but the problems are minor and do not appear to be 'fatal' problems.

Jim

idrees

6:13 am on Aug 5, 2009 (gmt 0)

10+ Year Member



Thanks for prompt reply. I have defined virtual hosts on both my server machine and local machine

server machine url: [website_name:8080...]
this will render page not found error

local machine url: [website_name...]
this will show a home page.

jdMorgan

2:16 pm on Aug 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Anything in the server error log?

It's likely that "noscriptname" or your app server (or something else) just isn't set up to work on a non-standard port -- i.e. anything but port 80. You might want to use a server headers checker to see if port 8080 requests are being redirected to port 80 and therefore 'breaking' things, and also do check your server error log: It will often tell you exactly what is wrong.

The code above can be more-efficiently and more-robustly written as:


RewriteEngine On
#
# Skip following rules if final URL-path-part contains a period
# (i.e. contains a filetype), except for ".html"
# (Modified to allow periods anywhere except in final URL-path part)
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule ^([^/]+/)*([^.]+\.)+([^./])+$ - [L]
#
# Internally rewrite requests for extensionless URLs to add ".html"
RewriteRule ^(([^/]+/)*[^./]+)$ $1.html
#
# Internally rewrite index requests to index.html
RewriteRule ^$ index.html
#
# Internally rewrite to web controller if .html file does not exist
# (else the existing .html file will be served)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Note that code like this which applies sequential rewrites can trigger a documented bug in mod_rewrite, causing parts of the URL-path to be duplicated unexpectedly. If this is happening, you'll see it in your error log as incorrect filepaths (containing repeated path-parts). If needed, the code above can be modified to eliminate the sequential rewriting to avoid this bug.

Note also that [QSA] is a waste of time unless you want to append new query data to the existing query data. If no new query data is included in the RewriteRule substitution, then the default behaviour is for all rules to simply pass the existing query string unchanged. Therefore [QSA] was not needed in any of these rules.

Jim