0

The 3 comments in the code explain fairly accurate what I want to achieve.

<IfModule mod_rewrite.c>
RewriteEngine On

# Change secretdiary.org/index.php?url=URL to secretdiary.org/URL on the browser's url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]

# Redirect http://www.secretdiary.org/ to http://secretdiary.org/
RewriteCond %{HTTP_HOST} !^secretdiary.org$ [NC]
RewriteRule ^(.*)$ http://secretdiary.org/$1 [L,R=301]

# Add trailing slash / if there's none
RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
</IfModule>

However, I am finding some problems and I think they come from putting the conditions together. When I enter www.secretdiary.org/about , it gets (showing it in the browser) to secretdiary.org/index.php?url=about, deleting the www but ignoring the first rule. Switching the order did not help at all nor messing with RewriteBase. However, if I enter normally without the www, the uri is shown normally, secretdiary.org/about, without any rewriting. Why is this and how can I fix it?

Besides, I've followed this answer and this other attempting to add automatically a trailing slash to the uri if missing. I could achieved it with PHP ( if (substr($_GET['url'], -1) != "/") header("Location: " . htmlspecialchars($_GET['url']) . '/');, but now it bothers me that I cannot achieve it with .htaccess, so if you could also spot where's the problem here it'd be very helpful.

Community
  • 1
  • 1
Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90
  • I would put the www. removal first. Remember that RewriteConds are regex's, so escape .'s `(\.)`. Can you get into any cases where you add a trailing / after anything **but** an actual directory? You shouldn't be doing that. Only directories should have /. – Phil Perry Jan 31 '14 at 20:08

2 Answers2

1

Try this .htaccess code :

RewriteEngine On

# Change secretdiary.org/index.php?url=URL to secretdiary.org/URL on the browser's url
RewriteCond %{HTTP_HOST} ^secretdiary.org$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]

# Redirect http://www.secretdiary.org/ to http://secretdiary.org/
RewriteCond %{HTTP_HOST} !^secretdiary.org$ [NC]
RewriteRule ^(.*)$ http://secretdiary.org/$1 [R=301]

# Add trailing slash / if there's none
RewriteRule ^([^/]*)[^/]$ $1/ [R=301,L]

I'm not sure for the last rule.

Lucas Willems
  • 6,673
  • 4
  • 28
  • 45
0

The main problem I faced was with Firefox storing the 301 redirect, which made the changes in .htaccess "not work". I deleted the cache and now it's working perfectly, although I made the trailing slash to be added with PHP to avoid headaches.

.htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On

# For some shady reason, this redirect should be first.
# Redirect http://www.secretdiary.org/ to http://secretdiary.org/
RewriteCond %{HTTP_HOST} !^secretdiary.org$ [NC]
RewriteRule ^(.*)$ http://secretdiary.org/$1 [L,R=301]

# Change secretdiary.org/index.php?url=URL to secretdiary.org/URL on the browser's url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>

index.php:

<?php
// Redirect if there's no trailing slash
if (!empty($_GET['url']) && substr($_GET['url'], -1) != "/")
  {
  header ('HTTP/1.1 301 Moved Permanently');
  header ("Location: http://secretdiary.org/" . htmlspecialchars($_GET['url']) . "/");
  }

// The rest of the php
Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90