Reputation: 581
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.
Upvotes: 1
Views: 264
Reputation: 785376
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]
Upvotes: 1