views:

62

answers:

3

Why does the following htaccess file generate 300 errors, when this URL is called?


URL: hxxp://subdomain.domainame.com/keyworda/

IndexIgnore *

RewriteEngine on
RewriteRule ^([a-z]+)/$ /index.php?p=$1


Error log: 300 hxxp://subdomain.domainame.com/keyworda (no trailing slash)

A: 

can you try this one:

RewriteRule ^([^/]+)/?$ /index.php?p=$1
Faisal Feroz
This doesn't work.
rrrfusco
+1  A: 

Edit, I was trying to keep my answer short, I didn't mean for you to add this line exactly. It was just to show where the missing slash should go.

RewriteRule ^/([a-z]+)/$ /index.php?p=

I'm assuming you are editing a .conf file, in your apache config directory. Chigley's answer is assuming you are editing a per-directory .htaccess config file.

Edit your conf file to look like this:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_METHOD} ^TRACE
  RewriteRule .* - [F]
  RewriteRule ^/(test[a-z]+)/$ /index.php?id=$1 [L,R=301]
  ... your current stuff afterwords ...
</IfModule>
  • I think the problem is you need the initial '/' in yoru RewriteRule.
  • Putting my test RewriteRule first will make sure it is the one matching.
  • I added in the "test" prefix, so this line wont interfere with the rest of your website. This will make it easier to isolate this rule. So, when testing, the url must end with a "/" and begin with "test" for it to match, in addition to having 1 or more a-z.
  • The url to test is: hxxp://...yourserver.../testkeyword/
  • This should change the url in your browser to: hxxp://...yourserver.../index.php?id=testkeyword
  • The 404 error you are seeing can mean: 1) No rules match, and there is no file that matches your keyword. 2) One or more rules match, but the resulting url does not match your php files.

Once it's working, this is the real RewriteRule to use in your conf file:

RewriteRule ^/([a-z]+)/$ /index.php?id=$1 [L,R=301]

If it does not work, edit your question to include the full code in the block, and your httpd version, like this:

# httpd -v
Server version: Apache/1.3.33 (Darwin)
Server built:   Mar 20 2005 15:08:27

Probably doesn't matter, but just incase...

Here is the mod_write documentation. There are some examples at the very bottom.

Brian Maltzan
Nope, that's not true.
chigley
What does the initial slash do? If I were to call hxxp://subdomain.domainame.com//keyworda/, would that rewrite to p=/keyworda ?
rrrfusco
The first slash will match the first one after your .com. The second slash will match the one after your keyword.
Brian Maltzan
Your answer throws a 404 error.
rrrfusco
A: 

Try

RewriteRule ^([a-z]+)/?$ /index.php?p=$1 [L]
chigley
This would be better if you explain L and R
Brian Maltzan
What's the difference between ? ungreedy modifier vs. not having the modifier?
rrrfusco
@rrrfusco - in this case, the question mark makes the trailing slash optional. So `http://subdomain.domainname.com/keyworda` and `http://subdomain.domainname.com/keyworda/` will be redirected.
chigley
If the ? modifier points both trailing and non trailing to index, how would you add redirect 301 for the non-trailing?
rrrfusco
`RewriteRule ^([a-z]+)/$ /$1 [R=301,L]``RewriteRule ^([a-z]+)$ /index.php?p=$1 [L]`
chigley
Your answer still generates 300 errors, but displays the correct page.
rrrfusco