j5Dev
j5Dev

Reputation: 2042

htaccess rewrite/redirect if last character is NOT a digit or slash

I have a site where I have a htaccess rule set to take the entire url, and forward it to my index file, using the below rule, with everything working fine.

#################################
# Magic Re-Writes DO NOT CHANGE #
#################################
<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine on
  #RewriteBase /

  # Do Not apply if a specific file or folder exists
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  # The rules on how to rewrite the urls

  RewriteRule (.*) /index.php?url=$1 [QSA,L]
</IfModule>

So the below rule forwards http://mydomain.com/players/scoresheet/singlegame

to

http://mydomain.com/index.php?url=players/scoresheet/singlegame

However, I also need to ensure I cater for people forgetting the trailing slash in the url, something normally straight forward, however, I need to be able to force the final trailing slash ONLY if that last character is not numerical (or a slash obviously).

For Example, someone types;

http://mydomain.com/players/scoresheet/singlegame

I need the url in the browser to show as: http://mydomain.com/players/scoresheet/singlegame/

but still be forwarded to: http://mydomain.com/index.php?url=players/scoresheet/singlegame/

As said the exception to this will be if the last character already has the trailing slash, or is a numerical digit.

(Hope that makes sense)

Ok, heres what I have so far...

#######################################
#      Add trailing slash to url      #
#  unless last character is a number  #
#######################################

<IfModule mod_rewrite.c>
  RewriteEngine on
  Rewritecond %{REQUEST_URI} [^0-9/]$
  RewriteRule ^(.*)$ /$1/ [R=301,L]
</IfModule>

#################################
# Magic Re-Writes DO NOT CHANGE #
#################################
<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine on
  RewriteBase /

  # Do Not apply if a specific file or folder exists
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  # The rules on how to rewrite the urls

  RewriteRule (.*) /index.php?url=$1 [QSA,L]
</IfModule>

The problem with this is although it seems to get the adding of the slash to the url, it also addes the index.php as well, so what I end up with, is:

Visit: http://mydomain.com/players/scoresheet/singlegame

get url rewritten to: http://mydomain.com/index.php?url=players/scoresheet/singlegame/

The slash is added, but I need it to do so without display the index part.

I have gone backwards and forwards, with many different outcomes (usually outright failures, or loops).

Any help would be appreciated

Upvotes: 3

Views: 2302

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

Your rule is correct, but it's blindly redirecting even when it's not supposed to. The URL that you have above is probably not what it's getting rewritten to. You have it as:

http://mydomain.com/index.php?url=players/scoresheet/singlegame/

But I'm willing to bet it's really something like:

# note the slash here--------v
http://mydomain.com/index.php/?url=players/scoresheet/singlegame/

Because after the URI is internally rewritten and routed to /index.php, the rewrite engine loops again and the redirect catches it, and redirects /index.php to /index.php/. So you need to add the same exclusion conditions that you have in your routing rule:

So change:

Rewritecond %{REQUEST_URI} [^0-9/]$
RewriteRule ^(.*)$ /$1/ [R=301,L]

to either:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Rewritecond %{REQUEST_URI} [^0-9/]$
RewriteRule ^(.*)$ /$1/ [R=301,L]

or:

RewriteCond %{REQUEST_URI} !index.php
Rewritecond %{REQUEST_URI} [^0-9/]$
RewriteRule ^(.*)$ /$1/ [R=301,L]

Upvotes: 2

Related Questions