Forum Moderators: phranque
I am having lots of troubles getting my .htaccess file to work with creating static urls.
I am trying to achieve the following file structure:
http://www.example.com/directory1/directory2/shot/2/
instead of:
http://www.example.com/directory1/directory2/shot_detail.php?shot_id=2
Here is my .htaccess code that I am trying to use (.htaccess file is located in the directory2 folder):
RewriteEngine On
RewriteRule ^shot/([^/]+)/$ /shot_detail.php?shot_id=$1 [L]
This gets a 404 error.
Having the "Options +FollowSymLinks" statement causes an 500 Internal Server Error.
I know very little about .htaccess coding (!) - could anyone offer any suggestions?
There's a link under the bread crumbs, and this post addresses what you are trying to do specifically: Changing Dynamic URLs to Static [webmasterworld.com]
I am trying to get the 301 redirect code to work for this same folder (again without luck).
Here is the code I am attempting:
RewriteEngine on
RewriteRule ^shot/([^/]+)/$ /directory1/directory2/shot_detail.php?shot_id=$1 [L]
RewriteCond %{THE_REQUEST} ^[0-9]+\ /shot_detail.php?shot_id=([^&\ ]+)\ HTTP/
RewriteRule ^shot_detail.php /directory1/directory2/shot/%1/? [R=301,L]
Can you see any obvious faults in this?
Thank you in advance.
As the current pattern only matches numbers for the HTTP request method and does not match any of the directory path, it won't likely work. Taking a guess, I'd say try:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /directory1/directory2/shot_detail\.php\?shot_id=([^&\ ]+)\ HTTP/
[edited by: jdMorgan at 11:03 pm (utc) on Nov. 8, 2009]
RewriteEngine on
RewriteRule ^shot/([^/]+)/$ /directory1/directory2/shot_detail.php?shot_id=$1 [L]
RewriteCond %{THE_REQUEST} ^[0-9]+\ /directory1/directory2/shot_detail\.php\?shot_id=([^&\ ]+)\ HTTP/
RewriteRule ^directory1/directory2/shot_detail\.php$ http://www.example.com/shot/%1/? [R=301,L]
# OR
RewriteRule ^shot/([^/]+)/$ /shot_detail.php?shot_id=$1 [L]
RewriteCond %{THE_REQUEST} ^[0-9]+\ /shot_detail\.php\?shot_id=([^&\ ]+)\ HTTP/
RewriteRule ^shot_detail\.php$ http://www.example.com/shot/%1/? [R=301,L]
# Only one of the preceding should be correct and the left side of the first rule should be the rewrite for the right side of the 2nd rule. So, if the 'visited URL' people are supposed to see is: http://www.example.com/directory1/directory2/shot/%1/ the preceding should be the right side of the second rule with a trailing ? and the left side of the first rule should be: ^directory1/directory2/shot/([^/]+)/$
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /directory-1/directory-2/shot_detail\.php\?shot_id=([^&\ ]+)\ HTTP/
But, as it is still not working - I believe my corresponding RewriteRule code must be invalid.
TheMadScientist - Thank you for looking at this, I am studying your advice/code further.
In /directory-1/directory-2/.htaccess :
# Externally redirect (only) direct client requests for internal filepath back to canonical URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /directory-1/directory-2/shot_detail\.php\?shot_id=([^&\ ]+)\ HTTP/
RewriteRule ^shot_detail\.php http://www.example.com/directory-1/directory-2/shot/%1/? [R=301,L]
#
# Internally rewrite canonical URL requests to script filepath with query string
RewriteRule ^shot/([^/]+)/$ /directory-1/directory-2/shot_detail.php?shot_id=$1 [L]
These two rules, in the context of /directory-1/directory-2/.htaccess are now mirror images of each other, with the qualification that the first rule is only invoked by direct client requests -- as is required to prevent an 'infinite' redirect/rewrite loop. But if you still have a problem, please try to be *very* descriptive about it: "It doesn't work" tells us almost nothing in addition to the fact that you likely wouldn't be posting if it did work.
Please be specific about what URL you tested, what you expected to happen, what actually happened, and how those differ. Any other info, such as relevant server access and error log entries, and HTTP header traces --when useful-- can/should be added.
In particular here, we need to know if both rules now fail, or only one.
Jim
Yes, the code being used in In /directory-1/directory-2/.htaccess is exactly the same as your previous code. In the same order.
The request in the access log is exactly as pasted in my previous post.
The URL I am typing into the browser (after clearing the cache) is:
http://www.example.co.uk/directory-1/director-2/shot_detail.php?shot_id=1
Thank you
"GET /directory1/directory2/shot_detail.php?shot_id=2 HTTP/1.1"
http://www.example.co.uk/directory-1/director-2/shot_detail.php?shot_id=1
Sorry about not noticing the 0-9 rather than A-Z in the match for THE_REQUEST... Lil copy / paste error.
Does this mean that HTTP/ would need to be substituted with HTTP/1\.1& ? No, the posted condition will 'implicitly' match /1.1 or /1.0 since it's not 'end anchored'.
If it's enabled but you don't use it, try adding
Options -MultiViews
Assuming you've made no transcription errors in URL-path patterns, the code is fine. You'll need to be looking for other rules in higher-level .htaccess and config files and/or other directives that rewrite URLs -- mod_alias, mod_proxy, mod_dir, mod_negotiation, etc. *Something* is grabbing that URL before your code in /directory-1/directory-2/.htaccess gets a chance to act on it.
Jim
Adding Options -MultiViews unfortunately causes a 500 Internal Server Error.
I have already been in contact with my hosting provider and asked them if there are 'any restrictions' that would mean the code in the .htaccess would not function, and their answer has always been no.
I am not sure where to go next with this.
This is the error message in the error log when Options multiviews is inserted.
I am thinking this must be an server configuration issue that is stopping this code from working.
I will be contacting my hosting company AGAIN to request more information on this.
My hosting does not allow me to view my own error logs, I have had to request them. I am now considering moving hosts.