Miguel Cardona Polo
Miguel Cardona Polo

Reputation: 182

How to add a new Rewrite rule in .htaccess file to target different files in the same folder?

This is my .htaccess file:

Options +FollowSymLinks -MultiViews
AuthType Basic
AuthName "website"
AuthUserFile ""
require valid-user


RewriteEngine On

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

I want to add the following:

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

Adding this line at the end produces a server error since they both match the same. Both rules aim to change the URL of different files in the same folder.

What I want is to hit the following link

website.com/y where y could load category.php?category=category1

and also have

website.com/x where x could load article.php?slugId=some-slug

article.php and category.php would be two different files in the same folder.

How can I change the rules to make them work?

Upvotes: 1

Views: 646

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133528

Could you please try following, written as per your shown samples.

In following solution:

  • Please change firststring with string which you want to look in URI while rewriting it to article.php in backend.
  • Change secondstring with string which you want to look in URI while rewriting it to category.php.

Options +FollowSymLinks -MultiViews
AuthType Basic
AuthName "website"
AuthUserFile ""
require valid-user

RewriteEngine ON

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1 !-f
RewriteCond %{REQUEST_URI} firststring [NC]
RewriteRule ^([^/]+)/?$ article.php?slugId=$1 [L]


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

Upvotes: 2

Related Questions