Using .htaccess, how can I strip parameters from a url and redirect an entire folder to another location?
For example, I have http://www.webbiscuit.co.uk/News.aspx/11/favicon-competition and I just want to redirect this to http://www.webbiscuit.co.uk/.
I have tried
Redirect 301 /News.aspx http://www.webbiscuit.co.uk/
but that just strips off the News.aspx part, and redirects me to
http://www.webbiscuit.co.uk/11/favicon-competition
How can I strip off the entire file and the slugs?
Using mod_rewrite (the ? may be unnecessary here):
RewriteRule ^News\.aspx(.*) http://www.webbiscuit.co.uk/? [NC,R=301,L]
Related
I bought an old site that apparently used some kind of a CMS, maybe wordpress.
Now it's a simple html site. I want to redirect www.example.com/main and everything in that directory to root. In other words, if a url is example.com/main/a_bunch_of_garbage that should 301 redirect to the homepage. Here is what I have but it doesn't quite work.
Options +FollowSymLinks
RewriteEngine on
RedirectMatch 301 ^/main/(.*)$ http://www.example.com
If the url doesn't contain any weird characters like ? or = then it works. But for example, if I go to www.example.com/main/index.php?option=com_content&view=post&id=84&Itemid=982/
Then although it does redirect to the homepage, in the address bar, I still see the entire URL. Can I fix this?
I also just remembered another problem I had. If I put example.com/main (without forward slash after main) the redirect doesn't work. It only works if I put example.com/main/ (with forward slash)
You need to use mod_rewrite rules only and avoid mixing RedirectMatch here. Have your rules like this in your root .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^main(/.*)$ $1? [L,NC,R=301]
? in the end is used to strip off any existing query string.
i have to redirect one url to another. http://www.abc.com/realestate/ to Redirect to http://www.abc.com/businessfinder/company/4105/Property_Agent/. is it better to change on the codeigniter routes.php or the .htaccess file?
If you know that a URL should be redirected then there is no reason to hand the request to PHP. So, redirecting using the .htaccess file would be preferable.
If you have mod_alias installed then you can use the Redirect directive:
Redirect 301 /realestate/ http://www.abc.com/businessfinder/company/4105/Property_Agent/
You can also use mod_rewrite to jump between places in various ways. E.g.:
RewriteRule ^/realestate/?$ /businessfinder/company/4105/Property_Agent/ [R=301,L]
If that is not possible then you can use CodeIgniter's $this->url->redirect() method. It will send the user to any website you like.
I have some dupilcate pages in my Joomla site.
Examples
/80-games
/80-games?start=12
/80-games?start=20
I did a 301 redirect in my .htcaccess for the first one which works fine.
My redirect for /80-games
Redirect 301 /80-games http://www.teach-this.com/esl-games
But for /80-games?start=12
The url changes to http://www.teach-this.com/esl-games?start=12
What should my redirect be for /80-games?start=12
Somehow the question mark is causing my destination url to change.
Thanks
Paul
You should use mod_rewrite as you cannot manipulate QUERY_STRING using mod_alias rules.
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^80-games/?$ http://www.teach-this.com/esl-games? [R=301,L,NC]
Note ? at the end of target URI that is used to strip out any existing QUERY_STRING in the original URL.
I had to adjust some paths on my site and I need to use .htaccess to redirect the items on the chance a user accesses the old url.
For example my old urls (relative) could be:
/old-path/page1.php
/old-path/page2.php
/old-path/page3.php
etc...
I had to change the path (for this example) to new-path and I need to adjust the .htaccess so anyone that comes to any page with .../old-path/... will be redirected to
.../new-path/...
Also, would this satisfy the 301 or would I need to list out each page?
You can use either mod+alias:
Redirect 301 /old-path /new-path
or using mod_rewrite:
RewriteEngine On
RewriteRule ^/?old-path/(.*)$ /new-path/$1 [L,R=301]
These could be in the htaccess file in your document root or in the server/vhost config. If you already have rewrite rules somewhere, you may just want to stick with mod_rewrite because redirecting with mod_alias while using mod_rewrite can sometimes give conflicting results.
I have a problem with creation a redirect 301 through htaccess for pages with parameters.
I want to redirect all pages in one directory to the homepage of another (the new URLs are without parameters and have a different structure so I just want to redirect to the main section)
It means - I have now
http://www.mysite.com/old_directory/page.php?s=12345
etc... and all of them I want to redirect to
http://www.mysite.com/new_directory/
How should I modify this redirect? Its Apache server and Wordpress platform.
If you end the substitution string with just a question mark, it will erase an existing query string. mod_rewrite
RewriteRule ^/old_directory/.* /new_directory/? [R=301,L]