Forum Moderators: phranque
I'm experiencing a weird problem when trying to rewrite URLs for my site. The problem also appears only on my Linux box, locally (Win+Apache2) everything works fine.
This is my .htaccess:
-----------
Options +FollowSymLinks
RewriteEngine on
RewriteBase /intern
RewriteCond %{REQUEST_URI} news$
RewriteRule ^(.*)$ news.php [L]
RewriteCond %{REQUEST_URI} news/post$
RewriteRule ^(.*)$ news_post.php [L]
---------
The .htaccess file is placed in folder intern in document root.
The first rewrite rule works without any flaw, but the second one does not work. Actually this is not quite correct, it only works under a strange circumstance:
If the URL is [domain.com...] the rewrite rule works. Why does it have to contain a double "news"? This doesn't make any sense to me.
Any help appreciated!
Thanks!
First, from your description, it doesn't sound like you need to use RewriteBase at all.
Second, there is no apparent need for your RewriteConds at all -- you can put the URL-path to be matched into the RewriteRule itself (leave off the leading slash in .htaccess context).
If this sounds wrong to you, then maybe you could describe exactly what you are trying to accomplish so that the code can be reviewed in that light.
I'd suggest:
Options +FollowSymLinks
RewriteEngine on
RewriteRule news$ /intern/news.php [L]
RewriteRule ^news/post$ /intern/news_post.php [L]
Jim
The reason it makes no difference whether you precede the RewriteCond pattern with "/news" is that your pattern was not start-anchored. Therefore, it would match any request to this directory that ends with "post".
Jim
thank you both for your quick answers.
@jdMorgan: I tried what you suggested, but it just doesn't work. It only works for the first rule, but as soon as I try to rewrite something like news/post it doesn't work anymore. Even more strange to me: It doesn't throw an error message, it just shows the news page as if it would have used the first rewrite rule.
This is my exact setup and what I'm trying to accomplish:
The server is a linux box running Apache2. My domain is an Apache2 virtualhost, so its path on the server is: /var/www/www.example.com/htdocs/.
The site I'm working on is located at: /var/www/www.example.com/htdocs/intern/.
My .htaccess also resides in this (intern) folder.
This is what I want:
My links look like this: http://www.example.com/intern/news/post
I want this link to be internally redirected to http://www.example.com/intern/news/news_post.php
This also has to work with variables later on, so I need to to use regular expressions. These are not my problem, since my first .htaccess worked fine at home on my Windows box.
I hope you can help me with this.
Thanks in advance!
Cheers!
Maybe mod_dir is affecting these URLs. Since they don't have a filetype and don't end in slash, mod_dir may grab them and stick a slash on the end.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^news/$ /intern/news.php [L]
RewriteRule ^news/post/$ /intern/news_post.php [L]
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^intern/news/$ /intern/news.php [L]
RewriteRule ^intern/news/post/$ /intern/news_post.php [L]
sorry to bother you again, but none of your ideas worked. Still the same problem, simple rewrites do work, but nothing alike /news/post.
Are there any options/settings for mod_rewrite you can set, that might be responsible for this? Or any Apache specific setting that may kick in when working with mod_rewrite? I even tried to disable mod_dir, but that didn't help either.
I'm pretty much out of ideas. And the strangest thing to me is, that everything worked fine on my local machine with my initial .htaccess file.
I'd be grateful if you or someone else could offer me any other advice or help.
Thanks!
I finally found the error, but do not know how to fix it.
The error occurs because links like news are automatically rewritten to news.php by Apache without even having a .htaccess file. How can I stop this behaviour?
To prove it I created a rule like RewriteRule test/post$ news_post.php [L] and it worked like a charme, so as soon as the first string after root is recognized by apache as a filename it rewrites the URL automatically. This renders my whole site linkstructure useless,
I could change the filenames to something different from the link or vice versa, but there must be an easier way.
Do you know what I can do?
Thanks in advance!
P.S. This is a really stupid error...