1

After much research, I've finally learned that mod_rewrite does not create "pretty links", rather it rewrites them to "normal" links that get processed by the server.

With some generous help here on SO, I have been able to do some basic URL rewriting, specifically removing the www. and also the .php file extension even when a user types in page.php. The .htaccess code for reference:

RewriteEngine On

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

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]

# To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

This website I'm working on has a news section, the links use a query string to pass the article id from links in the sidebar like so:

<a href="news.php?id=' . $phpVariable . '...etc.

After finding this amazing post HERE I began experimenting with this line of code from the post:

RewriteRule ^blog/([0-9]+)/([A-Za-z0-9-\+]+)/?$ /blog/index.php?id=$1&title=$2 [NC,L,QSA]

and modified it to match my website like so:

RewriteRule ^news/([0-9]+)$ news?id=$1 [NC,L,QSA]

Then I modified the link code in my news section to this:

<a href="news/' . $phpVariable . '/...etc.

so that the link would be "pretty" and be rewritten with the modified RewriteRule shown above.

The new RewriteRule was placed at the bottom of the .htaccess code (which may be part of the problem?) shown here:

RewriteEngine On

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

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]

# To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

# rewrite pretty link to fetch news article
RewriteRule ^news/([0-9]+)$ news?id=$1 [NC,L,QSA]

Now when the news section is tested, any item selected from the sidebar causes a 500 Internal Server Error.

I'm just a bit in over my head, though thanks to the information here on SO, the regex is beginning to make more and more sense to me.

Community
  • 1
  • 1
wordman
  • 571
  • 2
  • 6
  • 19
  • I just copied the `error.log` and it oddly, it shows nothing recent (only some minor php errors from the 27th of last month). The URL causing the error is the `news` page itself, `news/1/` for example, the first article having been selected. – wordman Mar 06 '17 at 19:24
  • Do you get 500 when you directly enter `http://example.com/news/1` in browser? – anubhava Mar 06 '17 at 19:29
  • Yes sir, I did. (I don't know why I didn't think to try that...thanks for the tip) – wordman Mar 06 '17 at 19:34
  • My syntax may not be completely on point. The Internal server Error page comes up with this at the bottom `Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.` so I assume(d) that this was a 500 error. – wordman Mar 06 '17 at 19:35

1 Answers1

1

Based on question and comments I will suggest you to have it this way:

ErrorDocument 404 default
ErrorDocument 500 default

Options +FollowSymLinks -MultiViews
RewriteEngine On

# omit www.
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301,NE]

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

# rewrite pretty link to fetch news article
RewriteRule ^news/(\d+)/?$ news.php?id=$1 [NC,L,QSA]

# To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
anubhava
  • 664,788
  • 59
  • 469
  • 547
  • I'm still getting an error but it is now missing the `Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.` part. What other information can I provide to better assist? – wordman Mar 06 '17 at 19:43
  • Yes, I copied the exact code in your answer. I've downloaded the error log but am having a fit uncompressing it. – wordman Mar 06 '17 at 20:02
  • Without knowing relevant error text from `error.log` we can't solve it. On the other side I've tested this .htaccess on my Apache and everything worked fine. – anubhava Mar 06 '17 at 20:12
  • Okay, I was able to access the log, here is what it said: `mod_rewrite: maximum number of internal redirects reached. Assuming configuration error. Use 'RewriteOptions MaxRedirects' to increase the limit if neccessary.` – wordman Mar 06 '17 at 22:17
  • Do you have this .htaccess inside a subdirectory by any chance? – anubhava Mar 07 '17 at 05:49
  • No sir, it's in the site root. That error log message has me wondering...is there a way to reset the number of redirects that it mentions? – wordman Mar 07 '17 at 06:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137494/discussion-between-wordman-and-anubhava). – wordman Mar 07 '17 at 20:56
  • 1
    This worked perfectly, @anubhava had me add `` directly below the opening `` tag to get my CSS styling to affect the other pages. Works!!! – wordman Mar 07 '17 at 21:21