0

I have a rewrite rule below in my .htaccess file in the root directory:

RewriteEngine On

# Exclude .css / .js / ...
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]

# Rewrite Rule
RewriteRule ^([^/]*)/([^/]*)$ /index.php?category=$1&product=$2

So the URL http://example.com/data1/data2 give me category=data1 and product=data2.

The problem is that the below URLs are not working:

http://example.com/data1          # Not Working (Page Not Found)
http://example.com/data1/data2/   # Not Working (Page Not Found)

But these URLs are working:

http://example.com/data1/         # Works -> category=data1
http://example.com/data1/data2    # Works -> category=data1 & product=data2

How can I redirect the first two URLs to the second one?

OR/AND

Do something that all URLs are redirect to the non-trailing slash one. So the URLs below:

http://example.com/data1/
http://example.com/data1/data2/

are redirect to these:

http://example.com/data1
http://example.com/data1/data2
Vahid
  • 3,384
  • 2
  • 35
  • 69

1 Answers1

2

To avoid trailing slashes alltogether, do a redirect

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R,L]

To match a URL with just one element, you might use a second rule

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?category=$1&product= [L]

or

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?category=$1 [L]

Putting all together gives

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R,L]

RewriteRule ^(.+?)/(.+)$ /index.php?category=$1&product=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?category=$1 [L]
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • Thank you for the answer. The first one works but I couldn't make the second one work. Would you please provide the full .htaccess content that make those redirection correctly? So I won't have duplicate contents and both URLs are redirect to non-trailing slash one. – Vahid Jun 27 '16 at 08:58
  • 1
    If you want to avoid duplicate content, you must do a redirect first. But then you don't need the trailing optional slash anymore. See reworked answer. – Olaf Dietsche Jun 27 '16 at 10:09
  • Thanks for your description and answer. – Vahid Jun 27 '16 at 10:24
  • I have problems with unicode characters in the URL. For example the URL "example.com/data1/data2" works fine but the URL "example.com/نموذج/مثال" is not working. Could you please help with the problem? – Vahid Jul 15 '16 at 11:04
  • 1
    Right now, I don't know. Usually the URL should be percent encoded, but I don't know if mod_rewrite sees the encoded version or not. If you have a specific problem, search for [tag:.htaccess] and UTF-8, e.g. http://stackoverflow.com/search?q=%5B.htaccess%5D+utf-8. One of the first questions is about [Regex utf-8 arabic](http://stackoverflow.com/q/31171175/1741542), maybe there are more. If none of these help, ask a new question. – Olaf Dietsche Jul 15 '16 at 16:11