1

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.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Jeff
  • 13
  • 3

2 Answers2

0

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]
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • I have put code and it's working.. thanks :). But other issue is generated because i would want to id from URL (get parameter).. Now getting error.. can you please help me. – Jeff Oct 03 '16 at 15:19
  • @Jeff Just interchange the `$1` and `$2` above. – hjpotter92 Oct 03 '16 at 15:47
  • I need help you.. can you do it – Jeff Oct 06 '16 at 11:26
  • @Jeff I already updateed my answer with the change of $1 and $2. – hjpotter92 Oct 06 '16 at 11:43
  • I have one page : http://dev.example.com/beweb/news.php . so in this page one link of read more.. that's called http://dev.example.com/beweb/newsdetails.php?id=8&title=iphone.. so whenever i run this url so I need set the error 404 not found page.. I have already set the rules as you provided – Jeff Oct 06 '16 at 11:50
  • @Jeff Sorry, I did not understand what you meant by that comment. You can probably [ask] another question, explaining in detail the requirement? If I understood you correctly, you want direct access to `newsdetails.php` pages to be restricted? – hjpotter92 Oct 06 '16 at 12:07
  • I will explain you in details.. please give me few mints. – Jeff Oct 06 '16 at 12:11
  • You did provide suggestion ..it's good and solved the issue for dynamic title and id. But I created one page that's called news.php.. and In this page some of information with title and image of data for news. In news data.. one link is called "Read More".. and set url dev.example.com/beweb/newsdetails.php?id=8&title=iphone Actually, we have coded when run url so it is redirected to http://dev.example.com/beweb/iphone-8.html.. That's fine – Jeff Oct 06 '16 at 12:28
  • But what exactly i need when url run : dev.example.com/beweb/newsdetails.php?id=8&title=iphone getting 404 not found page @hjpotter : I think.. we need to change code in htacess for Read More link.. Exactly we need to set automatically write url in this format : http://dev.example.com/beweb/iphone-8.html – Jeff Oct 06 '16 at 12:28
  • For that, you'd need to modify the `%{THE_REQUEST}` variable. Try to learn from the answer [here](http://stackoverflow.com/a/33978728/1190388). If you're still unable to achieve it, I'll try to answer here again. – hjpotter92 Oct 06 '16 at 13:03
  • I unable to achieve it.. Actually do you understood what exactly i want – Jeff Oct 06 '16 at 13:23
0

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

mahyard
  • 1,230
  • 1
  • 13
  • 34
  • Code-only answers are usually not sufficient. Add some explanation *why* your code works and why OP's code doesn't. Otherwise your answer can only help the OP who'll just copy-paste your answer - and that's not the way SO is meant to be used. – Al.G. Oct 03 '16 at 17:48
  • @Al.G. yes, you are right, I will add explanation soon. – mahyard Oct 03 '16 at 19:52