Reputation: 837
I have a web page that displays news content. The URL to the pages looks something like
example.com/news.php?newsid=iphone4-releases
. I want the page to be accessible through a URL like this: example.com/news/iphone4-releases
I made these changes in my .htaccess
file
RewriteCond %(REQUEST_FILENAME) !-d [OR]
RewriteCond %(REQUEST_FILENAME) !-f [OR]
RewriteCond %(REQUEST_FILENAME) !-l
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://example.com/news.php?newsid=$1 [NC]
This code actually replaces the URL, but I want to preserve the same URL without the GET variables. Also, using this method the URL becomes example.com/iphone4-releases
but I want example.com/news/iphone4-releases
Upvotes: 0
Views: 224
Reputation: 1758
This will do, assuming you will only use a-Z, digits and a dash in your article name:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^news/([a-zA-Z0-9\-]*)$ /news.php?newsid=$1 [NC]
Edit:
This link explains about the parameters: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond
And about the -l:
'-l' (is symbolic link) Treats the TestString as a pathname and tests whether or not it exists, and is a symbolic link.
And numbers will work in this example (eg. /news/iphone2003-new-launch)
Im sorry, but im a hurry :)
Upvotes: 1
Reputation: 38253
You can't expect to omit GET variables and use .htaccess to do a 303 redirect because the redirect will lose your variables. What you can do is submit a form with POST to the page you will be redirecting to and create a symlink from that page to the real PHP script, and then create a rewrite rule to redirect the client's browser.
Here's your above redirect, incorporating the added /news/
section:
RewriteEngine On
RewriteBase /
RewriteCond %(REQUEST_FILENAME) !-d [OR]
RewriteCond %(REQUEST_FILENAME) !-f [OR]
RewriteCond %(REQUEST_FILENAME) !-l
RewriteRule .* - [L]
RewriteRule ^(.*)$ news/news.php?newsid=$1 [NC]
You can also do a redirect using only PHP:
header("Location: http://www.redirecthere.com");
Upvotes: 1
Reputation: 32148
Maybe you will need to remove both [OR]
because you want the requested file not to be file AND not to be directory AND not to be a symbolic link
RewriteCond %(REQUEST_FILENAME) !-d
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-l
RewriteRule ^/(\w+)/(.*?)/?$ $1.php?newsid=$2 [NC,L]
and you don't need the whole domain to be inside your rule
Upvotes: 0