.htaccess Php重写网址

I try to realize a system of rewriting URLs in .htaccess.

Then here is my goal: If I have an url of this form: http://localhost/view.php?Id=456

Then I want to transform it to: http://localhost/456

I use this rule in htaccess:

RewriteRule ^ ([a-zA-Z0-9] +) $ view.php? Id = $ 1

Now this works very well!

But my problem I want to add points to id ie instead of 456 I can put: my.book

That is to say: http://localhost/my.book

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9\.]+)$ view.php?id=$1 [QSA,L]

You need RewriteCond %{REQUEST_FILENAME} !-f before the RewriteRule line to tell the server that the RewriteRule written below to be executed if the input passed in the URL is not an actual file. Because server searches for a file matching the input you pass in the URL and also it won't work in case you pass my.book in the URL since web server recognizes . as prefix for extension like .php or .html or like so and thereby it results in Not Found error if there is no file named my.book exists. So, you also need to escape . in the URL.

To allow .'s in the input, you need to add . with escape sequence \ in the character class group like ^([a-zA-Z0-9\.]+)$. Note, allowing this can result in escaping the extension in the URL, that is, passing view.php in the URL won't navigate to the actual file. Rather, it will be considered as a value in the query string.

Try this:

RewriteRule ^([a-zA-Z0-9\.]+)$ view.php?Id=$1

Basically what I did is I added \. with your pattern. This will make sure your regex matches any letter (small/caps), decimal numbers and periods (.). Hope this helps :)