简体   繁体   中英

Using .htaccess to Rewrite URLs with ID and Filename

I am trying to rewrite a URL with .htaccess and had a question that I'm unable to find an answer to after many hours of searching. And, it doesn't help that my regex skills aren't that great...

The underlying format for my URLs is this: http://mysite.com/article.php?id=1 .

I would like to have a URL like this direct there: http://mysite.com/article/1/made-up-irrelevant-title-slug .

The problem is, I'd like the article to be a variable such that it could be changed to page and would direct to something like: http://mysite.com/page.php?id=1 , from http://mysite.com/page/1/made-up-page-name .

If anyone knows how this is properly written, I would be extremely grateful!

# Turn on Rewrites
RewriteEngine on

# Do not rewrite existing files, directories, or symlinks.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

# Rewrite /COMPONENT/ID/TITLE -> COMPONENT.php?id=ID
RewriteRule ^([^/]+)/(\d+)/.*$ $1.php?id=$2 [L]

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