简体   繁体   中英

How to change php urls on my website?

Is it possible to somehow change my website links from:

domain.com/category.php?tag=test
domain.com/section.php?tag=test
domain.com/news.php?tag=test

into this:

domain.com/category-test
domain.com/section-test
domain.com/news-test

Thanks and have a good day!

You can achieve this using .htaccess and mod_rewrite . You'll need to create a .htaccess file with the following contents:

RewriteEngine On
RewriteBase /

RewriteRule ^category-([^/]+)/?$ category.php?tag=$1
RewriteRule ^section-([^/]+)/?$ section.php?tag=$1
RewriteRule ^news-([^/]+)/?$ news.php?tag=$1

Now, accessing domain.com/category-test/ will take you to category.php?tag=test .

If you'd prefer to have slashes instead of dashes, you can use:

RewriteRule ^category/([^/]+)/?$ category.php?tag=$1
RewriteRule ^section/([^/]+)/?$ section.php?tag=$1
RewriteRule ^news/([^/]+)/?$ news.php?tag=$1
RewriteRule ^category-([a-zA-Z0-9]+)$ category.php?tag=$1 [NC,L]
RewriteRule ^section-([a-zA-Z0-9]+)$ section.php?tag=$1 [NC,L]
RewriteRule ^news-([a-zA-Z0-9]+)$ news.php?tag=$1 [NC,L]

In "category.php" file get the tag $_GET['tag'];

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