regex.htaccesshttp-redirectmod-rewriterapache

.htaccess redirect URL with id param to human friendly URL


I'm trying to create a URL redirect but so far everything I have tried just hasn't shown any effect on the site. I know ModRewrite is enabled as there are other rewrites taking place. The whole purpose of this is to handle old URLs from the former version of the website.

What I want to achieve is a redirect of a URL with the following format:
/resources/view?id={id} and redirect it to /resources/{id}.

I've been trying to do so with variants of this:

RewriteRule ^resources/view?id=([0-9+])$ /resources/$1 [R=301,L]

and also this:

RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^/resources/view$ /resources/$1? [R=301,L]

Cheers.


Solution

  • You can use these 2 rules in your site root .htaccess:

    Options -MultiViews
    RewriteEngine On
    
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} /resources/view\?id=(\d+) [NC]
    RewriteRule ^ /resources/%1? [R=301,L,NE]
    
    # internal forward from pretty URL to actual one
    RewriteRule ^resources/(\d+)/?$ resources/view?id=$1 [L,QSA,NC]