简体   繁体   中英

Unable to write a rewrite rule in .htaccess for my php website

My website is v2.example.com and I am trying to write a .htaccess rule but unable.

I want this : v2.example.com/ABCDEFGH

And I want to get the value ABCDEFGH as a parameter like it is v2.example.com/index.php?id=ABCDEFGH

can anyone please help me sorting out this problem.

I tried this :

<IfModule mod_rewrite.c>
RewriteEngine On 
RewriteBase /
RewriteRule ^(.*) index.php?id=$1 [L]
</IfModule>

Please help me.

RewriteEngine on
RewriteRule ^(.*)$  index.php?id=$1 [L,QSA]

this will crash as it goes into infinite loop, as index.php is further rewritten to index.php?id=index.php and so on

solution

RewriteEngine on
RewriteRule ^([^_]*)$   _index.php?id=$1 [L,QSA]

that is create page _index.php , and rewrite all pages not having _ to this page, this way it will not go into infinite loop, you can choose ~ instead of _ if you think you will need _ in ur parameters

Have your rule like this:

<IfModule mod_rewrite.c>
RewriteEngine On 
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+index\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?id=$1 [L,QSA]
</IfModule>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM