1

I'm trying to set Semantic URLs via .htaccess (on local server). I've already replaced index.php?id=# with page# by the means of:

RewriteRule ^/?page([0-9]+)$ index.php?id=$1

It is working. That means that .htaccess is connected properly and runs. Now I want to "glue" the produced link with the original one. That is, to redirect from index.php?id=# on page#. Can't deal with it.

For example, I take only the page with id=0 and make this:

RewriteCond %{HTTP_HOST} ^site\.loc\/index\.php?id=0$ [NC]
RewriteRule ^(.*)$ http://site.loc/page0$1 [R=301,L]

Tell me, please, what is wrong with the rows above.

  • 1
    `%{HTTP_HOST}` really just contains the vhost name, not the URL path. You'd want to match `QUERY_STRING` and/or the `REQUEST_URI` per RewriteRule simply. – mario Aug 14 '15 at 12:19
  • I've tried also something like this: RewriteCond %{REQUEST_URI} ^\/index\.php [NC] RewriteCond %{QUERY_STRING} id=(\d+) [NC] RewriteRule .* http://site.loc/page%1? [R=301,L]. Didn't help. – Анатолий Демьяненко Aug 14 '15 at 12:46

2 Answers2

0

See also https://stackoverflow.com/a/31280108/345031 on "Ping-Pong" rewrites and how to enable the RewriteLog (or compare access/error.log) to debug such issues.

Your approach was on the right track. But I think for the old-to-new URL redirects you should cut it down to:

                                optional   escape \?
                                   ↓         ↓
RewriteCond %{REQUEST_URI} ^/(?:index\.php)?\?id=(\d+)$
RewriteRule .* http://example.org/page%1 [R,END]
                                             ↑
                                      Safer than [L] flag

The REQUEST_URI contains the ?-separated QUERY_STRING already. So the RewriteCond can match both. It's best to optionalize the index.php there, because that's not part of the incoming request URL.

(Capturing the page=id parameter per \d+ and %1 was already correct.)

Just use the [END] flag for both Ping-Pong rules. Make your existing internal/pretty-URL RewriteRule last. Otherwise they'd likely interact.

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291
0

This should work :

RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?id=([^\s]+) [NC]
RewriteRule page%1? [NC,R,L]
RewriteRule ^/?page([0-9]+)/?$ index.php?id=$1 [NC,L]
Amit Verma
  • 40,709
  • 21
  • 93
  • 115