skip to Main Content

I previously had below which worked ok,

RewriteEngine on
RewriteRule ^([0-9]+)/?$ /news/index.php?post=$1 [L,QSA]

But I don’t want it to open like this

https://example.com/news/index.php?post=123456

I want it to open only in such a way that it only allows numbers

https://example.com/news/123456

is the request that /news/index.php?post=123456 should not resolve Only /news/123456 should be accessible

Thanks

2

Answers


  1. Hi to achieve this add the below lines and restart the services, where The provided rule ensures that the URL starts with "news/" and captures numeric values to append them to the URL as your expectation.

    RewriteEngine on
    RewriteRule ^news/(d+)$ /news/index.php?post=$1 [L,QSA]
    
    Login or Signup to reply.
  2. You need a combintation of both, an internal rewrite and an external redirection:

    RewriteEngine on
    # externally redirect direct requests to the actual resource
    RewriteConf %{QUERY_STRING} ^post=(d+)$
    RewriteRule ^/?news/index.php$ /news/%1 [R=301,L]
    # internally rewrite requests to pretty URLs
    RewriteRule ^/?news/(d+)$ /news/index.php?post=$1 [END]
    

    It is a good idea to start out using a R=302 temporary redirection and to only change that to a R=301 permanent redirection once everything works as expected. That prevents caching issues with clients while you are still trying around.

    Above rules will work likewise in the central http server’s configuration and in a distributed configuration file (".htaccess"). If you have access to the central configuration then that should clearly be the preferred place to implement such rules.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search