0

I'm wondering how I can redirect this:

sample.com/issues/3
sample.com/issues/4
etc.

to

sample.com/issues/index.php?id=3
sample.com/issues/index.php?id=4
etc.

UPDATE: A friend suggested this:

RewriteRule ^issues/(.*)$ issues/index.php?id=$1 [L]

But this redirects to the wrong subdirectory. E.g. it redirects /issues/4/ to /issues/4/?id=4

Strangely it seems to check out on this testing site but it's not working on my local server, nor on my live server.

Any ideas? The full .htaccess now looks like so:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^sample.com [NC]
RewriteRule ^(.*)$ http://www.sample.com/$1 [L,R=301]

Redirect /archive.php /issues/

RewriteRule ^issues/(.*)$ issues/index.php?issue=$1 [L]
Kai
  • 157
  • 1
  • 1
  • 9

1 Answers1

0

Create a new .htaccess inside issues/ directory with this rule:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?issue=$1 [L,QSA]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks. Do I keep the above code in the .htaccess file in the root directory? Update: with the original .htaccess in the root and your suggested .htaccess in the 'issues' folder, nothing happens. It just loads sample.com/issues/3 as usual. – Kai Oct 29 '18 at 13:01
  • Yes, I'm just asking if you want me to remove anything from the .htaccess file in the root directory? If I leave as is and add your .htaccess file to the /issues/ folder as you suggested, the browser simply loads sample.com/issues/3 with no apparent redirect happening at all. – Kai Oct 29 '18 at 13:42
  • That error is coming from your PHP code due to include path issue. For a browser and web server a valid issue URL `https://www.densediscovery.com/issues/2/` is loading same as invalid issue URL i.e.`https://www.densediscovery.com/issues/20/` . Note that you need trailing slash for all these URLs. – anubhava Oct 29 '18 at 14:10
  • Thanks for your help. I think I'm too far out of my depth now. Will have to hire someone to get this working. Cheers – Kai Oct 29 '18 at 14:21
  • Use this answer to fix your include path: https://stackoverflow.com/questions/8726898/using-htaccess-to-rewrite-paths-to-php-includes – anubhava Oct 29 '18 at 14:24