Daryl T
Daryl T

Reputation: 41

htaccess rewrite rule only partially works

I have a basic htaccess rewrite file, however I am unable to get one of the rules to work when it is similar to a previous rule.

My code is;

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^gallery$ gallery.php
RewriteRule ^about-us/page-1$ page-1.php
RewriteRule ^about-us/page-2$ page-2.php
RewriteRule ^about-us$ about-us.php

gallery, page-1 and about-us all work, however page-2 does not.

Is there a way to get this to work?

I have tried a different order and if about-us is put first, neither page-1 or page-2 work

note: The redirect redirects to the page-2 however the included files (css, js etc) do not load

Upvotes: 2

Views: 158

Answers (2)

Gunaseelan
Gunaseelan

Reputation: 2542

Add [L,NC,QSA] for the below 2 lines

RewriteRule ^about-us/page-1$ page-1.php
RewriteRule ^about-us/page-2$ page-2.php

Thus the modified .htacces will look like this

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^gallery$ gallery.php
RewriteRule ^about-us/page-1$ page-1.php [L,NC,QSA]
RewriteRule ^about-us/page-2$ page-2.php [L,NC,QSA]
RewriteRule ^about-us$ about-us.php

As Starkeen said in comment, For css and js file to work on the rewritten url add the below code to head of your document.

<base href="yoursitename.com/">;

Upvotes: 1

Amit Verma
Amit Verma

Reputation: 41219

Try this:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /about-us/page-2$ 
RewriteRule .* page-2.php [NC,L]    
RewriteRule ^gallery$ gallery.php [NC,L]
RewriteRule ^about-us/page-1$ page-1.php [NC,L]
RewriteRule ^about-us$ about-us.php [NC,L] 

Upvotes: 2

Related Questions