Home > OS >  One page available at two addresses .htaccess
One page available at two addresses .htaccess

Time:07-11

Initial data:

The site has some page:

 http://domen.com/index.php?act=rules

Decided to change the view of the links in the address bar of the browser to:

 http://domen.com/rules 

I make it a rule:

 RewriteRule ^([A-Za-z0-9]{3,10})$ index.php?act=$1 [L]

Everything works fine, there are no problems. (in the browser addresses bar the address you entered http://domen.com/rules and the script receives the act=rules parameter, because the page script uses it).

Problem:

The problem is that the same page is now available at these two different addresses.

How to make changing the addresses bar when entering at:

http://domen.com/index.php?act=rules to http://domen.com/rules

also with the preservation of the act=rules parameter? (because it is used in a script).

What rule can be used?

Or maybe is the logic itself wrong?

Tell me where to look. Thanks!

CodePudding user response:

As I understand your question you simply want to add a rule to redirect the original URL to the rewritten one?

This should do what you want:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^act=([A-Za-z0-9]{3,10})$
RewriteRule ^/?index\.php$ /%1 [QSD,R=301,L]
RewriteRule ^/?([A-Za-z0-9]{3,10})$ /index.php?act=$1 [END]

It is a good idea to start with a R=302 temporary redirection. And to only change that to a R=301 permanent redirection once everything works as desired. That prevents nasty caching issues.

You can implement those rules in the http server's host configuration (preferred place). Or, if you do not have access to that, you can use a distributed configuration file (".htaccess").

  • Related