Rewrite URL dynamic with title and id

感情迁移 提交于 2019-12-11 05:37:47

问题


I need to rewrite url dynamically with title and id. I want to make URL dynamically

http://dev.example.com/beweb/iphone-8.html

Instead of

http://dev.example.com/beweb/newsdetails.php?id=8&title=iphone

When i run the above URL so it is made dyanamically what i need.. but getting error

" Not Found
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
The requested URL /beweb/iphone-8.html was not found on this server."

I have used below code :

RewriteEngine On
RewriteCond %{THE_REQUEST} /beweb/newsdetails.php\?id=([^&\s]+)&title=([^&\s]+) [NC]  
RewriteRule ^ /beweb/%2-%1\.html? [NC,R,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^%2-([^.]+)\.html$ /beweb/newsdetails.php?id=$1&title=$2 [QSA,L,NC]

Please help me.


回答1:


The rules are almost correct. You just misconfigured the last statement, which should be:

RewriteRule ^([^-]+)-([^.]+)\.html$ /beweb/newsdetails.php?id=$2&title=$1 [QSA,L,NC]



回答2:


With these lines you can check the path that is not a real static file or directory on the server and then if negetive, proccess the file name to serve your user:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^beweb/([a-z]+)\-([0-9]+).html$ /beweb/index.php?id=$2&title=$1 [QSA,L,NC]

QSA means to add the query string to the new URL



来源:https://stackoverflow.com/questions/39834656/rewrite-url-dynamic-with-title-and-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!