简体   繁体   中英

Adding text to rewrite rule

So I am creating a URL shortener and in-order for this to work I will have to change my .htaccess file to change the URL from website.com/?id=myidhere to website.com/myidhere . I have sorted this task out for numerals but I have no clue how to perform it for letters. This is my .htaccess so far:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /(index\.php)?\?id=([0-9]+)([^\ ]*)
RewriteRule ^ /%3?%4 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ /index.php?id=$1 [L,QSA]

So it rewrites the numerals but when i enter text in the id of the url it shows a 404 error. Would someone be able to help me out? Thanks.

我不确定它能否解决您的问题,但是要支持数字和/或文本,您的正则表达式必须为[a-zA-Z0-9]+

尝试这个

RewriteRule ^myidhere$ id=myidhere [NC,L]

try this .htaccess code

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ /index.php?id=$1 [L]

Now you can access your webpage from

website.com/myidhere

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