Search code examples
apache.htaccesshttp-redirect

How to Redirect domain.com/something/1000


I have a website built on Laravel.

I have a chain of URLs:

  • example.com/something/1000
  • example.com/abc/1000
  • example.com/xyz/1000

Above URLs never existed on the website but Google is somehow noticing them.

Is there a way through .htaccess to redirect all those kind of URLs to:

  • example.com/something/1000 to example.com/something
  • example.com/abc/1000 to example.com/abc
  • example.com/xyz/1000 to example.com/xyz

Many thanks!

I am expecting a .htaccess code.


Solution

  • but Google is somehow noticing them.

    If Google is actually indexing them then they must have existed (at least at some point) on your site. By "exists", we mean return's a "200 OK" response code.

    If these URLs really do not exist and return a "404 Not Found" response then there is nothing you really need to do here as a 404 is the correct response and is enough to prevent search engines from indexing those URLs.

    If these URLs have been erroneously linked to by a third party (and Google is perhaps crawling these URLs) then you can implement a redirect.

    Something like the following using mod_rewrite at the top of the root .htaccess file would redirect URLs of the form /<something>/1000 to /<something>, covering the 3 examples you've listed:

    RewriteEngine On
    
    # Redirect "/something/1000" to "/something"
    RewriteRule ^([^/]+)/1000$ /$1 [R=301,L]
    

    Note that this matches URLs containing a single path-segment, followed by /1000 at the end of the URL-path. The $1 backreference contains the captured URL-path before /1000.