Rewrite URL dynamic with title and id

2019-07-23 11:50发布

问题:

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