0

I'm using Rewrite Rule in htaccess to get page data by slug.

I'm doing it manually by adding lines to .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !www\.example\.co\.il$ [NC]
RewriteRule ^(.*)$ http://www.example.co.il/$1 [L,R=301]
ErrorDocument 404 /404.php
RewriteRule ^עמוד-מסוים$ /page.php?id=1 [L]
RewriteRule ^דוגמא-לעמוד$ /page.php?id=2 [L]
RewriteRule ^דוגמא-נוספת$ /page.php?id=3 [L]
RewriteRule ^טקסט-כלשהו$ /page.php?id=4 [L]
RewriteRule ^צרו-קשר$ /contact.php [L]
RewriteRule ^blog$ /blog.php [L]

It's work good but I need to add new .htaccess line every time.

my DB table has Id and Slug (and other info) and my page.php use $_GET['id'] and then select the other data from DB by this ID.

I tried somthing like this:

.htaccess:

RewriteRule ^page/([^/]*)$ /page.php?id=$1 [L]

page.php:

$id=$_GET['id'];

    if(is_int($id)==false){ // if slug was enterd
        $result=$mysqli->query("SELECT `id` FROM `pages` WHERE  `slug`='$id' limit 1");
        while($row=mysqli_fetch_assoc($result)){
            $id=$row['id'];
        }//query
    }

But the URL not look like I want (adding /page/ to url)

I tried that also:

.htaccess:

RewriteRule ^/([^/]*)$ /page.php?id=$1 [L]

but it's make problem with other url's on my site (that are not connected to page.php)

How can I make it work without adding htaccess line every time?

EDIT: when I try this .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !www\.example\.co\.il$ [NC]
RewriteRule ^(.*)$ http://www.example.co.il/$1 [L,R=301]
ErrorDocument 404 /404.php
RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L]
RewriteRule ^צרו-קשר$ /contact.php [L]
RewriteRule ^blog$ /blog.php [L]

I get error on all pages, If I moving up the RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L] only the page.php file will work good (blog and contact will not work) and also non-WWW url's will not work good.

itay
  • 357
  • 4
  • 16

2 Answers2

0

You could try adding some conditions, something like:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L]

to filter out all existing files and directories.

Also, you might want to consider sanitizing your $id parameter before you stick it in a query

Community
  • 1
  • 1
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • I'm getting Internal Server Error when try this. – itay Dec 26 '14 at 23:17
  • Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, webmaster@example.co.il and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log. Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request. Apache Server at www.example.co.il Port 80 – itay Dec 26 '14 at 23:21
  • I found the problem but I don't konw how to solve it. I have those line before the line you gave me: RewriteCond %{HTTP_HOST} !www\.example\.co\.il$ [NC] RewriteRule ^(.*)$ http://www.example.co.il/$1 [L,R=301] If I remove them I'm not getting the error – itay Dec 26 '14 at 23:28
  • @itay you need to place any routing rules **after** redirect rules, so these need to go at the VERY end. YOu need to look at the server *logs*, not the error message you get in your browser. – Jon Lin Dec 26 '14 at 23:39
  • my mistake. it's not work good. If I move the HTTP_HOST to the bottm it's work for the page.php but do problem for other files that not in page.php and make problem if type the url witout WWW... so It's not solved.. – itay Dec 27 '14 at 00:00
  • You cannot move those conditions. `RewriteCond` will only apply to the immediately following rule, they aren't global in any way. You must have the two conditions that I have right above the rule, and all 3 lies **must be at the very end** – Jon Lin Dec 27 '14 at 01:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/67745/discussion-between-itay-and-jon-lin). – itay Dec 27 '14 at 09:02
0

Found the soultion. This working perfect:

ErrorDocument 404 /404.php 
RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteRule ^צרו-קשר$ /contact.php [L]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L]
itay
  • 357
  • 4
  • 16