Htaccess [SOLVED]: How to redirect/block old URLs

Htaccess [SOLVED]: How to redirect/block old URLs

Home Forums htaccess Htaccess [SOLVED]: How to redirect/block old URLs

Tagged: ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36958

    Anonymous

    QuestionQuestion

    I created my .htaccess file

    Options +FollowSymlinks -Indexes
    RewriteEngine On
    RewriteRule ^login/$ views/sessions/login.php [L]
    RewriteRule ^([a-zA-Z]+)/$ views/sessions/$1.php [L]
    .... (a dozen line of rewriting)
    

    The links on my site work like this:

    mysite.com/login/ to access mysite.com/views/sessions/login.php

    But now I want to block access to old links.
    When the user goes to mysite.com/views/sessions/login.php it returns a 404 error or returns it directly to the link mysite.com/login/

    How to block old URLs?

    I added [R=301,NC,L] (RewriteRule ^login/$ views/sessions/login.php [R=301,NC,L])

    but when I want to access to mysite.com/views/sessions/login.php

    I have this error : “Forbidden You don’t have permission to access /C:/wamp/www/mysite/views/sessions/login.php on this server.”

    #36959

    Anonymous

    Accepted AnswerAnswer

    You can add one query string parameter while rewriting url, see the below code. I have added one query string parameter for redirecting the url.

    RewriteCond %{REQUEST_URI} !^/views/sessions/([a-zA-Z]+).php$ [NC]
    RewriteRule ^(.*)/$ views/sessions/$1.php?s=old [L]
    

    To redirect old urls : (old urls will be redirected to new urls)

    RewriteCond %{QUERY_STRING} !^(.*&)?s=old$ [NC]
    RewriteRule ^views/sessions/([a-zA-Z]+).php$ $1/ [R=302,NC,L]
    

    To block the old urls : (old urls will be blocked)

    RewriteCond %{QUERY_STRING} !^(.*&)?s=old$ [NC]
    RewriteRule ^views/sessions/([a-zA-Z]+).php$ $1/ [R=401,NC,L]
    

    Edit: (adding query string to new urls)

    RewriteCond %{REQUEST_URI} !^/views/sessions/([a-zA-Z]+).php$ [NC]
    RewriteRule ^(.*)/$ views/sessions/$1.php?%{QUERY_STRING}&s=old [L]
    

    &s=old is not compulsory, its just to apply condition for blocking the old urls. you can take any variable instead of it, choose it as it does not affect to your program logic.

    Source: https://stackoverflow.com/questions/47993528/how-to-redirect-block-old-urls
    Author: ravisachaniya
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.