user3263978
user3263978

Reputation: 193

mod_rewrite not working in htaccess

In my .htaccess file which is in the root of my directory, I have this;

RewriteEngine on
RewriteBase /

RewriteRule ^website-comments/webid/([^/]*)$ /website-comments?webid=$1 [L]

But for some reason it is not rewriting the URL, if what I'm thinking is correct then if a user visits www.website.com/website-comments?webid=1 it should change the url to www.website.com/website-comments/webid/1 but it doesn't seem to be working, am I doing something wrong? Thanks in advance

EDIT 1

Other HTACCESS RULES

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

Upvotes: 1

Views: 93

Answers (3)

Phil Perry
Phil Perry

Reputation: 2130

if what I'm thinking is correct then if a user visits www.website.com/website-comments?webid=1 it should change the url to www.website.com/website-comments/webid/1

No. The incoming URI "from the wild" (typed in by a user, supplied by a bookmark or search engine, or a link in your site) is going to be the SEO form: /website-comments/webid/1. The job of .htaccess is to change that to the dynamic format with a Query String, which your PHP script can digest (read the element in $_GET): /website-comments.php?webid=1. So, you have it backwards.

RewriteEngine On
RewriteRule  ^website-comments/webid/([0-9]+)/?$  /website-comments.php?webid=$1 [L]

would be one way to do it, and may meet your needs.

Upvotes: 1

Michael Berkowski
Michael Berkowski

Reputation: 270757

Apache's mod_rewrite when used with basic rules can silently serve an internal URL to your users, but it cannot enforce the use of that internal URL with just the same simple rule. Typically if you want your end users to never use the actual URL (/website-comments?webid=123 in your case) you need to first provide a rule which redirects away from that URL by matching it in the original HTTP request using Apache's variable %{THE_REQUEST}.

RewriteEngine On
# probably don't actually need RewriteBase
RewriteBase /

# First match the URL which the user requested. If it is ?webid=N
# do an actual redirection to the pretty URL
# This will grab digits from the query string to use in %1
RewriteCond %{THE_REQUEST} webid=(\d+)
# ...only doing this for website-comments -- a permanent redirect to the pretty URL
# so the client makes a new request to the pretty URL that won't match the above condition 
# on the next trip.
#
# Since the [R] will automatically append the old query string, add a ?
# onto the end to clear out the query string so this rule doesn't match again and
# go into an infinite loop.
RewriteRule website-comments /website-comments/webid/%1? [L,R=301]

# Then apply your existing rule to rewrite internally to the target resource
RewriteRule ^website-comments/webid/([^/]*)$ /website-comments?webid=$1 [L]

The above (\d+) assumes that webid will only be an integer. If that's not true and it could include other characters, consider using ([^&]+) instead to match everything up to the next & or end of the string.

Upvotes: 1

TomGrill Games
TomGrill Games

Reputation: 1593

try this (switch) RewriteRule parameters

RewriteRule ^/website-comments?webid=([^/]*)$ website-comments/webid/$1[L]

Upvotes: -1

Related Questions