简体   繁体   中英

Rename Url .htaccess

I'm having trouble with mod-rewrite to rewrite these url:

mywebsite.com/index.php?page=folder/file
mywebsite.com/index.php?page=folder/file&id=10

into these

mywebsite.com/file
mywebsite.com/file/10

How can I do that?

You want somthing similar to this

#With mod_rewrite
RewriteEngine on
RewriteRule   ^/file/(.+)  /index.php?page=folder/file&id=$1 [R,L]
RewriteRule   ^/file       /index.php?page=folder/file [R,L]

mywebsite.com/index.php?page=folder/file
mywebsite.com/index.php?page=folder/file&id=10
into these
mywebsite.com/file
mywebsite.com/file/10

It is not clear if folder is a fixed string or the directory where file is, so in this answer it is assumed to be a fixed string as the directory in both cases is root.

You may try this in one .htaccess file at root directory:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}  !index\.php  [NC]
RewriteRule  ^([^/]+)/?$  /index.php?page=folder/$1 [L,NC]

RewriteCond %{REQUEST_URI}  !index\.php  [NC]
RewriteRule  ^([^/]+)/([^/]+)/? /index.php?page=folder/$1&id=$2  [L,NC]

Maps silently:

http://mywebsite.com/file to
http://mywebsite.com/index.php?page=folder/file

And

http://mywebsite.com/file/10 to
http://mywebsite.com/index.php?page=folder/file&id=10

Where strings file and 10 are assumed to be dynamic.

For permanent redirection, replace [L,NC] with [R=301,L,NC]

Do you mean you want to rewrite and NOT TO REDIRECT mywebsite.com/$var into mywebsite.com/index.php?page=folder/$var and mywebsite.com/$var1/$var2 into mywebsite.com/index.php?page=folder/$var1&id=$var2 ? You can check this:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^([a-z0-9-_]+)$ /index.php?page=folder/$1

RewriteRule ^([a-z0-9-_]+)/([a-z0-9-_]+)$ /index.php?page=folder/$1&id=$2

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