I have run into an issue with my .htaccess file.
The file changes the ugly URL such as http://localhost/news.php?article_slug=example-1
to http://localhost/news/example-1
This works perfectly, but when I go to http://localhost/news
i get a 404 error.
Within news.php I have a redirect; so if there is not an article slug in the URL it will redirect to latest.php.
my PHP code on news.php
$article_slug=$_GET['article_slug'];
if (empty($_GET)) {
header("Location: ../latest.php");
die();// no data passed by get
}
This is what I currently have in my .htaccsess file
Options -MultiViews
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteRule ^news/([wd-]+)$ /news.php?article_slug=$1[QSA,L]
RewriteCond %{QUERY_STRING} article_slug=([wd-]+)
RewriteRule ^news$ /news/%1 [R,L]
RewriteRule ^category/([w-]+)$ /category.php?category_slug=$1&page=$2 [QSA]
When I try to debug this myself (with very little knowledge) and add the following line it redirects to latest.php
RewriteRule ^([^/]*)/?$ /news.php [L,QSA]
but on the redirected page I get the following error
The page isn’t redirecting properly
Firefox has detected that the server is redirecting the request for this
address in a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept cookies
When I use the developer tools in firefox as IMSoP commented all I see is latest.php reloaded multiple times.
This is not just isolated to just latest.php but any file on the server thats not listed in the access file
when I remove the line
RewriteRule ^([^/]*)/?$ /news.php [L,QSA]
I can load the PHP file but it doesn’t redirect from news.php and http://localhost/news
is not found but http://localhost/news/example-1
works.
2
Answers
None of your rules catch such a request, so no rewriting occurs and you get a 404.
This rewrites everything to
/news.php
, including/latest.php
that you are redirecting to in your PHP script and the cycle repeats, resulting in a redirect loop to/latest.php
.However, this redirect in your PHP code would also seem to assume there is a slash on the original request. ie. should be
/news/
(with trailing slash) not/news
(no trailing slash) as you state in the question.It would be better to redirect to a root-relative (or absolute URL) in your PHP script. ie.
header('Location: /latest.php');
(Note you are missing a space before the "flags" argument in the first rule.)
The first rule can be modified to allow
news/
and not justnews/<something>
. This is achieved by simply changing the quantifier from+
(1 or more) to*
(0 or more) on the capturing subpattern.The second rule is not currently doing anything. You should probably be targeting
news.php
here. But the rules are also in the wrong order.As noted in my answer to your earlier question, the
d
shorthand character class is not necessary, sincew
(word characters) already includes digits.Try the following instead:
Note, the first rule should be a 301 (permanent) redirect, not a 302 (as it was initially). But always test first with a 302 to avoid potential cachining issues.
This allows requests to
/news/
, but not/news
(no trailing slash). Only one of these can be canonical. If you need to handle/news
as well then you should redirect to append the trailing slash (so/news/
is canonical, and the URL you should always link to.) For example, before the above two rules:Summary
Bringing the above points together:
However, the same now applies to your
/category
URL. This was also discussed in your earlier question. (I’ve removed the superfluous&page=$2
part and added the missingL
flag.)If you have many such URLs that follow a similar pattern (eg.
news
andcategory
etc.) you don’t necessarily need a separate rule for each. (An exercise for the reader.)UPDATE:
As discussed in comments, this should read:
Rewrite rules are at heart very simple: they match the requested URL against a pattern, and then define what to do if it matches.
The pattern here translates as "must match right from the start; anything other than a slash, zero or more times; optional slash; must match right to the end". The action if it matches is to act as though the request was for "/news.php", adding on any query string parameters.
That’s a very broad pattern; it will match "news" and "news/" but it will also match "hello-world", and "__foo–bar..baz/". The only thing that would stop it matching is other rules higher up your config file.
Meanwhile, every time this rule matches, your PHP code in news.php will run, and if there isn’t anything on the query string, will tell the browser to request "latest.php".
But the rule will also match "latest.php". So when the browser requests "latest.php", the code in "news.php" gets run, and tells the browser to request "latest.php" again … and we have an infinite loop.
The simplest fix is just to make your rule more specific, e.g. look specifically for the word "news":
Another common technique is to add a condition to the rule that it only matches if the URL doesn’t match a real filename, like this: