Forum Moderators: phranque
I've got 3 rules - the second 2 are working, only the first variable is required. The last variable is a chain of numbers and letters that are hyphenated. It will always follow the pattern of 'some number'-some-descriptive-words-number-varies. I tried writing the last rule and finally tried an htaccess generator. Neither was successful. I keep getting a 404 error when I try to use the link.
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.php$ /index.php?page=$1&sets=$2&works=$3 [L,NC]
RewriteRule ^([^/.]+)/([^/.]+)/?$ /index.php?page=$1&sets=$2 [L]
RewriteRule ^([^/.]+)/?$ /index.php?page=$1 [L]
I want to be able to enter below:
[mydomain.com...]
php/html ending is optional - the rewrite tool I tried required it
The first two variables are working (see second 2 rules), but when I include the 3rd variable, I get the 404. If I enter this url:
[mydomain.com...]
it works. So, my php is working, but it doesn't appear to be getting the last variable properly when I pass it the shortened url.
Any assistance or direction appreciated! :-)
RewriteRule ^([^/]+)/([^/]+)/([^/.]+)(\.php)?$ /index.php?page=$1&sets=$2&works=$3 [NC,L]
Jim
Thanks much for the fast response. I've tried cleaning cache a couple of times and even another browser. I just keep getting 404. I'm sure there's something really dumb I'm doing here.
If I pass it a long URL, my PHP works - I assume this means it's correct, but is that a definite?
This is my complete htaccess - just in case there's something in here that's contradicting the rewrite.
Options +FollowSymLinks
RewriteEngine On
ErrorDocument 404 /404.php
RewriteCond %{HTTP_HOST} !^www\..* [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/.]+)(\.php)?$ /index.php?page=$1&sets=$2&works=$3 [NC,L]
RewriteRule ^([^/.]+)/([^/.]+)/?$ /index.php?page=$1&sets=$2 [L]
RewriteRule ^([^/.]+)/?$ /index.php?page=$1 [L]
Included your clean up suggestion.
Still looking to see if I can figure this out. Thanks again!
I decided to go for the take it apart and put it back together route - the line in my htaccess that's causing this issue is:
RewriteCond %{HTTP_HOST} !^www\..* [NC]
When I took it out, the url I was trying to pass works... weird.. My understanding was the above forced a "www" beginning to the site... Obviously there's more to it than I know. Is the above unncessary or is there a way I should edit it?
Thanks again!
Do that redirect as a completely separate action before you do any of the rewrites.
.
Also be aware that your two (-f and -d) conditions apply only the single RewriteCond that directly follows them.
If you need those two conditions to also apply to the other two rules, then you have to duplicate those conditions before each and every rule that they apply to.
Add a blank line after each and every RewriteRule line and then add a comment line (# comment) after that explaining exactly what the next block of code does.
In this case, the three of them together say, "Invoke the following RewriteRule only if the requested hostname does not start with "www." and the requested URL-path does not resolve to an existing file or directory.
You should be aware that the file-and-directory-exists checks are very expensive computationally, since they may actually result in a directory read operation on the physical disk. It is better to avoid that if possible, even at the expense of having to check a relatively long list of URL-paths that you *know* you do (or don't) want to rewrite (You can do it either way), or finding some other 'cue', such as using extensionless and trailing-slashless URLs for those that are to be rewritten to your script, and then checking to be sure that the requested URL has no trailing slash and does not contain a period (indicating that a filetype is present) after the final slash in the URL-path. By doing this, you only have to do the file/directory-exists checks if the requested URL is non-canonical (non-preferred, or incorrect but correctable), and you greatly reduce the number of potential disk directory reads per user 'session'.
The file-and-directory-exists check method is a one-size-fits-all solution used by WordPress, Joomla, and many other off-the-shelf blog, forum, and CMS systems. They use it because it works and because it doesn't require that their customer know anything about their own Web site and its URL and file-structure. However for the reason explained above, it can easily become the single most important factor in requiring a server upgrade once a site becomes successful and the number of requests skyrockets. So many folks probably end up paying big money to upgrade to a VPS or a dedicated server because their site has bogged down under the increased load, when the actual problem is that two additional disk reads are being invoked for each and every page, image, multimedia file, css file, JavaScript file, etc. requested from the site.
Because of the events which have unfolded here, allow me to give some advice: Before using any code in your server configuration, make sure that you understand every directive, every pattern, and even every character in it. Understand what all of the effects of the directives are, and how they will affect how visitors and search engines view your site. Proceeding without doing so can place you at risk of taking your server down all at once -- or if you are particularly unlucky, slowly eroding your search ranking over time due to some subtle bug or side-effect. Caution, much consideration, and very thorough testing is advised. The resources cited in our Forum Charter may be helpful in taking your code apart one character at a time to be sure you understand everything that it is doing. If your site produces important revenue, this is a wise investment of your time.
Jim