My .htaccess doesnt work

My .htaccess doesn’t work fine, only work three rules from four.

RewriteEngine On
RewriteBase /

RewriteRule ^prensa$ /prensa.php?seo=prensa [L]
RewriteRule ^/?$ /index.php [L]
RewriteRule ^noticias$ /prensa.php?seo=noticias
RewriteRule ^([^\.\?/]+)?$ /pagina.php?seo=$1 [L]

For example:

when I write www.myweb.com/bodas work well and get www.myweb.com/pagina.php?seo=boda

the same with www.myweb.com/noticias www.myweb.com/prensa.php?seo=noticias

but when I write www.myweb.com/prensa I get www.myweb.com/prensa.php, I dont return the right solution. Why?

I read http://datakoncepts.com/seo but I dont know my htaccess works so bad.

Thanks.

monchy,

Three for four isn’t a bad average! Take heart in that.

I’m definitely not fond of the RewriteBase directive as that generally causes more problems than it resolves but your first three cases are good.

RewriteRule ^([^\\.\\?/]+)?$ /pagina.php?seo=$1 [L]

Here you’ve begun the character range definition by escaping the dot character (which is NOT required in a character range definition) then by escaping a ?. Unfortunately (and the reason it’s not working for you), the ? is a reserved character (http://www.ietf.org/rfc/rfc2396.txt) which cannot be within a {REQUEST_URI} string (you may be able to encode it but I’d have to check the link above to verify that) because it’s the demarcation character defining the end of the {REQUEST_URI} string and the start of the {QUERY_STRING}. Of course, if you need to access the query string in mod_rewrite, you must do so with a RewriteCond statement.

So, other than not understanding the allowed content of URIs, I’d say that you’ve done very well.

Regards,

DK

Thanks dklynn,

I understand and I change. My case is so strange because if I change the first rule for this:

RewriteRule ^Prensa$ /prensa.php?seo=prensa [L]

If I change prensa for Prensa all is right, why?

I feel like there must be more to this htaccess than what we’re seeing, because as is, it looks like it should work correctly.

My case is so strange because if I change the first rule for this:

RewriteRule ^Prensa$ /prensa.php?seo=prensa [L]

If I change prensa for Prensa all is right, why?

Does your URL in the address bar have a capital P?

I’m doing a cms and I can change de url in the admin so I can put any word in the url. I dont understand what is the problem.

monchy,

Jeff simply didn’t read the character definition in your code correctly. Saying that, I’m sure that he does know that RewriteRules cannot examine the query string AND that the question mark is a reserved character (which should only be in a URL to separate the URI from the query string).

It makes no matter whether you want to match the P or p so long as you are consistent. If you want to match both, use

RewriteRule ^P|prensa$ /prensa.php?seo=prensa [L]

With the OR bar between the P and p, the regex will match both Prensa and prensa (if the only content of the URI string).

Regards,

DK

Actually I read the character definition just fine. Matching on non-question mark characters isn’t causing any issues. It may be unnecessary, but it’s not problematic.

Furthermore, the OP’s issue isn’t related to the forth rewrite rule, but the first. As the OP described, prensa is supposed to rewrite to /prensa.php?seo=prensa, but instead it’s rewriting to just /prensa.php. I reiterate that I don’t see anything in this htaccess that explains this behavior, nor do I see anything that explains why using a capital “P” in the URL would make it work. And so I still believe that there must be more to this htaccess than what we’ve seen.

I promise that I worte all the .htaccess. About capital letter is so strange but work.

I have another doubt how to use ExpiresActive in my htcaccess i put:

<IfModule mod_expires.c>
  # Enable expirations.
   ExpiresActive On
   ExpiresDefault A3600
  <filesmatch ".(jpg|JPG|gif|GIF|png|css|ico|js)$">
               ExpiresDefault "access plus 7 day"
 </filesmatch>
</IfModule>

RewriteEngine On
RewriteBase /

RewriteRule ^P|prensa$ /prensa.php?seo=prensa [L]
RewriteRule ^/?$ /index.php [L]
RewriteRule ^noticias$ /prensa.php?seo=noticias
RewriteRule ^([^\\.\\?/]+)?$ /pagina.php?seo=$1 [L]

Don’t work, when I try to access, return and error 500 internal server error. I use phpinfo() to know if the module mod_expires.c and I think that yes Thanks.

I dont know why but the error appears when I write anything in the if sentence

<IfModule mod_expires.c>
  # Enable expirations.
   
  <filesmatch ".(jpg|JPG|gif|GIF|png|css|ico|js)$">
               
 </filesmatch>
</IfModule>

If I put this I have no errors.

monchy,

[standard rant #4][indent]The definition of an idiot is someone who repeatedly does the same thing expecting a different result. Asking Apache to confirm the existence of ANY module with an <IfModule> … </IfModule> wrapper is the same thing in the webmaster world. DON’T BE AN IDIOT! If you don’t know whether a module is enabled, run the test ONCE then REMOVE the wrapper as it is EXTREMELY wasteful of Apache’s resources (and should NEVER be allowed on a shared server).[/indent][/standard rant #4]

Okay, that’s NOT to call you an idiot, it’s simply to remind you in the clearest terms possible that any webmaster would only run ONE server test, not repeat server tests multiple times for EVERY request!

You also need to learn which characters are considered metacharacters with a character range definition as it seems that you’ve gone “escape crazy” by escaping every character (none of which needs escaped except the ? which should not be there in the first place).

You might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It’s helped may members and should help you, too.

Regards,

DK

Obligatory note:

Unfortunately anyone who learns from this tutorial will have to unlearn some incorrect information later on. I strongly recommend that anyone who’s interested in this topic go straight to the definitive source for the most complete, authoritative, and correct information.

Jeff,

Sorry, I’d have to give the same advice to others about your insistence on using (.*) inappropriately (among other things you’ve come up with).

As for going straight to Apache.org, I agree. However, their documentation is too “geeky” for most so my tutorial was written to make life easier for SitePoint members … and has received high praise from everyone (other than you - your comment is the first of its nature in the 5+ years it’s been online).

Members,

Take all advice here with a grain of salt, i.e., consider the source.

Regards,

DK

My “insistence” on using .* comes straight from the Apache documentation. They use it in the same way I do.

Unfortunately your tutorial, in a number of places, contradicts the documentation, and it contradicts observable behavior in Apache.

/x.php exists, and a RewriteRule to from /x to /x.php doesn’t work. Hmm, that rings a bell.

DK, we’ve seen this bugger before quite a few times, you should recognize the smell of this stinky old friend by now.

@monchyrcg; start your .htacces with


Options -MultiViews

and everythig will work as planned.

Seriously, I still don’t understand why that option even exists in Apache, let alone why someone would turn it on :nono:

Ahh, yes! I think that would explain all the strangness we’ve been seeing. :slight_smile:

Rémon,

Good catch! MultiViews is one of those hidden traps that people can find themselves buried in and I agree that there is no logical reason to ever have it enabled.

Regards,

DK