0

I am rewriting all URLs and shorthand some of them:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(([^\.]+))\.pl [NC]
RewriteRule ^(.*)$ http://%1.pl%{REQUEST_URI} [R=301,L]
RewriteRule ^test$ test-page.php [R=301,L]

so that example.pl/test redirects to example.pl/test-page.php

but example.pl/test/ redirects to example.pl/

while it should: example.pl/test/ redirects to example.pl/test-page.php

How to handle both /url and /url/ redirects?

I assume that it has to something with trailing slash.

anubhava
  • 761,203
  • 64
  • 569
  • 643
Szymon Toda
  • 4,454
  • 11
  • 43
  • 62

2 Answers2

1

Try this:

RewriteEngine On
RewriteRule ^(.*)/$ http://%{HTTP_HOST}/$1 
RewriteRule ^test$ test-page.php [R=301,L]

Tested here.

BAD_SEED
  • 4,840
  • 11
  • 53
  • 110
1

You need to make trailing slash optional using /?$ regex. Try these rules:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.pl [NC]
RewriteRule ^ http://%1.pl%{REQUEST_URI} [R=301,L]

RewriteRule ^test/?$ test-page.php [NC,R=301,L]

Make sure to test this in a new browser to avoid 301 caching issues.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • What I have more rewrites like that? While `/?$` works, I would be happy with global solution rather than per-rule – Szymon Toda Jan 04 '14 at 14:18
  • You can have any # of rewrites. Let me know what specific URLs you want to rewrite? – anubhava Jan 04 '14 at 14:19
  • What do you mean by `All of requests`? Is this answer not working for you? You need to provide specifics for this answer isn't working so that I can suggest other ways. – anubhava Jan 05 '14 at 04:11