0

My pages looks like this:

www.mywebsite.com/work-view?id=1

www.mywebsite.com/work-view?id=2

www.mywebsite.com/work-view?id=3

and so on...

How do I set them like this with .htaccess?

www.mywebsite.com/work/1

www.mywebsite.com/work/2

www.mywebsite.com/work/3

I've already tried like this, but it doesn't work.

RewriteRule work/(.*)/ work-view.php?id=$1

EDIT:

Here's the new code that I'm trying.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteRule ^/?work/([0-9]+)$ work-view.php?id=$1 [L]

When I check the page /work-view?id=2 it displays fine, but when I try to enter /work/2 it shows 500 Internal Server Error. Unfortunately on cPanel logs I don't see any error related with this issue, maybe because of some hosting restrictions.

NEW EDIT:

arkascha answer works great, the problem was related with CSS and in my case when I use .htaccess I've changed the paths for the CSS and it's working great.

Here's another code that works great too.

RewriteRule   ^work/([0-9]+)/?$  work-view?id=$1    [NC,L]    # Handle product requests  
user2519032
  • 819
  • 1
  • 18
  • 34
  • 3
    There are already about 549264935 answers to this question alone here on StackOverflow. None of all those answers helped? _Why not_? And why should the 549264936th answer suddenly make a difference? Are you _really_ sure that you looked through the answers here? For example by starting with the section "Related" on the right hand side? And did you _really_ look into the documentation of the tool you use? – arkascha Apr 19 '16 at 20:48
  • Yes, I've already searched and I didn't find the answer for my case. If you know the solution, it will be useful to answer. – user2519032 Apr 19 '16 at 20:51
  • 1
    Sure, we'd love to help. But we need to know _why_ all those answers did not apply to your situation. So _why_ your case is different. Otherwise we would only end up writing the 549264936th answer which would be exactly like the others, so of no help for you, most likely. That is frustrating for both sides. – arkascha Apr 19 '16 at 20:52
  • 1
    Possible duplicate of [URL rewriting with PHP](http://stackoverflow.com/questions/16388959/url-rewriting-with-php) – visevo Apr 19 '16 at 21:15

1 Answers1

2

Just a short hint on the attempt you mention at the end of your question:

That pattern work/(.*)/ certainly will not match a subject work/1. Take a look at the trailing slash in the pattern. Instead you should try something like ^/?work/([0-9]+)$ or similar:

RewriteEngine on
RewriteRule ^/?work/([0-9]+)$ work-view.php?id=$1 [L]

And a more general note, I often point this out since surprisingly many developers do not seem to be aware at all about the issue: you should only use .htaccess style files if you really have to. It usually is the far better alternative to place such rewriting rules in the real host configuration of the http server. .htaccess style files are notoriously error prone, hard to debug and the really slow the server down, often for nothing. They are only provided as a last option for those who do not have control over the host configuration, for example when using a cheap hosting provider.

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • I've tried that too before and it doesn't work. Here's the code : RewriteRule ^/?work/([0-9]+)$ work-view.php?id=$1 – user2519032 Apr 19 '16 at 20:59
  • And "doesn't work" means what _exactly_? Wrong rewriting? An error in your log file? No rewriting at all? A blank page? The universe implodes? Keep in mind that we cannot look over your shoulder. _Be precise please_. The pattern certainly is fine. So maybe your situation actually is different from what you posted? – arkascha Apr 19 '16 at 21:01
  • OK, sorry for that. I mean when I enter www.mywebsite.com/work/2 it shows 500 Internal Server Error. – user2519032 Apr 19 '16 at 21:02
  • Ah, ok! That is actually an information. So you will have to take a look into your http servers error log file where you can read what the exact issue is. – arkascha Apr 19 '16 at 21:02
  • To be more precise the page /work-view?id=2 works fine and when I try with /work/2 it shows the 500 error. – user2519032 Apr 19 '16 at 21:03
  • Sure, `/work-view?id=2` is not rewritten. So what do you see in the error log file? – arkascha Apr 19 '16 at 21:04
  • I've just checked the cpanel log errors and I can see there only old errors and not related with the 500 error. – user2519032 Apr 19 '16 at 21:09
  • A http error send out will _always_ result in an entry to the log files. Actually at least two: one in the access log stating the http status and one in the error log stating the reason. `cPanel` sounds like you are using some hosting provider. Some of those only visualize log file entries delayed by 5 minutes, whyever. – arkascha Apr 19 '16 at 21:12
  • I've just read another topic about the error with the same situation. It might have some restriction from the hosting provider and that's why I cannot see the internal errors in the cPanel logs. I've waited couple of minutes, but nothing...bummer... – user2519032 Apr 19 '16 at 21:16
  • I've found the problem. Your code is right and the new path of the page was opening without CSS. When I change the css path, everything works great. I'm marking your answer, +1 and I'll post another approach that works great too. Thanks! – user2519032 Apr 25 '16 at 11:14