Forum Moderators: phranque
I wonder if someone could help me with a redirect please?
I'm already using the following .htaccess rules in order to remove index.php from the URL's that my CMS generates:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
My website has an Events section, which can be found at mywebsite.com/events/. As well as the main events page, there's also hundreds of other pages within that URL structure, i.e. mywebsite.com/events/test-event/ mywebsite.com/events/test-event-2/ etc
So what I'd like to do is, redirect every web page that is or starts with mywebsite.com/events/ to a special message page, i.e. mywebsite.com/special-message/
I've tried using this:
# Re-direct away from Events pages
Redirect 301 /events/ [mywebsite.com...]
But I found that although it works great for the main events page, it doesn't for all the hundreds of other pages that start with myhwebsite.com/events/...
Any help on this would be appreciated.
Thanks,
Ste
Please remember that we don't know anything about youe site, or what you expected or desired, so "it doesn't work" tells us little...
I'd suggest this, as a fix for your external redirect and a non-trivial speed-up of your internal rewrite:
RewriteEngine On
#
RewriteRule $1 ^events/ http://www.example.com/special-message/ [R=301,L]
#
RewriteCond $1 !^index\.php/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Jim
Thanks for the reply and the .htaccess code, much appreciated.
I found that my original redirect would redirect mywebsite.com/events/<anything>/ to mywebsite.com/special-message/<anything>/ I was originally trying to forward to the home page of my website though, and was trying all sorts of combinations, so I'd take your word for it if you were to say that I probably had a syntax error in my original code. Thanks for the RewriteRule - it looks fairly 'simple', but I was looking at all sorts of example and couldn't get anything like this working myself. :)
I'm also really interested in what you suggest for the internal rewrite, as this is code that I just mindlessly copy and paste into the .htaccess file as a matter of habit when building websites using the ExpressionEngine.com CMS. By default, it inserts index.php into the URL of web pages, so you would have mywebsite.com/index.php/about/ instead of mywebsite.com/about/
I can't even remember where I got this original code from, but it looks to be via this page as there's a very similar example there: [expressionengine.com...]
I've just tried your alternative suggestion and it also works. I couldn't notice any speed improvements, but if you tell me that it's reducing requests, then that's got to be a good thing and would no doubt show more benefit during peak times of website traffic on my web server (I'm using a VPS with Apache 2.0.63 and PHP 5.2.10).
Thanks again,
Stephen
To demonstrate, these two mod_alias and mod_rewrite lines are functionally equivalent:
Redirect 301 /index. http://www.example.com/home.
RewriteRule ^index\.(.*)$ http://www.example.com/home.$1 [R=301,L]
By manually excluding index.php from this check, you save at least one unnecessary call to the operating system's file-handler (and possibly an additional physical disk read if there's a lot of swapping going on).
By also excluding image, CSS stylesheet, and JavaScript file URL-paths, etc., you can greatly reduce the number of these unnecessary filesystem calls.
Jim
RewriteEngine On
RewriteCond $1 !^index\.php/
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]