Home > front end >  Redirect URLs with page version to URL without version except css and js
Redirect URLs with page version to URL without version except css and js

Time:12-03

I have some links on my website with page version and I want visitors redirected to the same page but without version number.

I got help before by {arkascha} with this code in .htaccess to affect all links, but i couldn't modify it to make exception for css and js files because visitors cannot load new version of css and js on my site because the code.

RewriteEngine on
RewriteCond %{QUERY_STRING} ^v=\d $
RewriteRule ^ %{REQUEST_URI} [QSD,R=301,L]

example pages with version

example.com/folder/locatedpage?v=1
example.com/folder/locatedpage?v=2
example.com/folder/locatedpage?v=3

example page after the code applied through htaccess

example.com/folder/locatedpage

I want the same code above but with exception for css and js files.

CodePudding user response:

RewriteRule ^ %{REQUEST_URI} [QSD,R=301,L]

Change the RewriteRule pattern to exclude .css and .js files. For example:

:
RewriteRule !\.(css|js)$ %{REQUEST_URI} [QSD,R=301,L]

The ! prefix negates the regex, so it is successful when it does not match. (Whereas ^ effectively matches everything.)

However, you will need to clear your browser cache before testing, since the 301 (permanent) redirect (that erroneously applied to .css and .js files) will have been cached persistently by the browser (and possibly intermediary caches). Preferably test with 302 (temporary) redirects to avoid caching issues.

  • Related