Reputation: 3598
In the small portfolio website I am making I want to have clean urls. For this I use a .htaccess file. At the moment my website runs at 127.0.0.1/website. My .htaccess looks like this:
RewriteEngine On
RewriteBase /website
RewriteRule ^about$ index.php?page=0 [L,NC]
RewriteRule ^portfolio$ index.php?page=1 [L,NC]
RewriteRule ^cv$ index.php?page=2 [L,NC]
RewriteRule ^contact$ index.php?page=3 [L,NC]
RewriteRule ^.*$ about [L,R]
If I remove the last line, everything works fine. I can go to 127.0.0.1/website/about
, and the server shows me 127.0.0.1/website/index.php?page=0
, while the url does not change. However, I want every other url to be redirected to 127.0.0.1/website/about
, and I tried to do that with the last line. However, if I now try to go to 127.0.0.1/website/anything
, where anything is any string, it redirects me to 127.0.0.1/website/about?page=0
. In addition to this, I would expect 127.0.0.1/website/about
to be handles fine, because of the Last flag at the first rewrite rule, but this redirects me to the same bad url.
I am clueless of why this does not work, and confused, because to me it seems like it is skipping the Last flag. I hope someone can point out what I am doing wrong. Thanks in advance.
Upvotes: 0
Views: 89
Reputation: 12469
This might work:
RewriteEngine On
RewriteBase /website
RewriteRule ^about$ index.php?page=0 [L,NC]
RewriteRule ^portfolio$ index.php?page=1 [L,NC]
RewriteRule ^cv$ index.php?page=2 [L,NC]
RewriteRule ^contact$ index.php?page=3 [L,NC]
RewriteCond %{REQUEST_URI} !^/cv [NC]
RewriteCond %{REQUEST_URI} !^/about [NC]
RewriteCond %{REQUEST_URI} !^/contact [NC]
RewriteCond %{REQUEST_URI} !^/portfolio [NC]
RewriteRule ^.*$ /about [R,L]
It will redirect everything that isn't /cv
, /about
, /contact
or /portfolio
to /about
.
Upvotes: 1
Reputation: 478
last rule rewrites everything so pattern must be more specific
RewriteRule !^index\.php$ about [L,R]
Upvotes: 1