How to redirect a webpage with .htaccess - .htaccess

I have this code in my htaccess but it is not working.
RewriteEngine on RewriteBase /
RewriteRule viewtopic\.php?t=([^/]+)$
http://newdomain.com/viewtopic.php?t=$1
How to redirect the page of viewtopic.php?t=1 to viewtopic.php?t=2000 to my new domain?

In addition to Daniel Gehriger's solution, add [L,R=301] at the end of the line. This will make Apache send the 301 Page Moved code to your client, informing it that the page has indeed moved permanently. This will cause search engines, RSS readers etc update their links (if they are clever enough).

You need to escape the . and ? in the pattern.
RewriteEngine on
RewriteBase /
RewriteRule viewtopic\.php\?t=([^/]+)$ http://newdomain.com/viewtopic.php?t=$1 [L,R=permanent]

just incase someone here have problem as mine, i use this code to redirect an html page to a sub domain:
RewriteEngine on
RewriteBase /
RewriteRule sharecrypt.html http://cryptool.sharepirate.com [L,R=301]

Related

force index.php in url with htaccess

I've spent the last two hours reading stackoverflow and the apache manual trying to find a solution to this, but haven't found any, if a directory is access (http://domain.ext) I'd like to force index.php to appear in the url (http://domain.ext/index.php) if possible.
If anyone could help I'd really appreciate it, thank you.
RewriteEngine on
RewriteRule index.php
you can use this:
RewriteEngine on
RewriteRule ^$ /index.php?
(but you will not see it in a browser because the rewrite engine just directs to the correct file/directory without changing the url from the browser)
with [R] tag you can redirect the user so the new url would appear in the browser
I've just found what works for me
RewriteEngine On
RewriteRule ^/?$ $1/folder/index.php$2 [R=301,L]

.htaccess redirects : posting the subsequent uri

I have a website set up where I store all my user profiles in /member-centre/member-directory/USERNAME
My problem is that a number of referrals are posting to a depreciated structure of /members/USERNAME
Is there a way I can configure my .htaccess file to automatically post the requested USERNAME to the correct location? All i've found on google is static redirection.
If this is not possible through .htaccess, is there another method I could use?
Many thanks in advance
If you are looking for a redirect solution, Jon Lin's will work fine. If you want to do a rewrite then put the following in your .htaccess file in the root of your domain
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/members/([^/]+)$ [NC]
RewriteRule . member-centre/member-directory/%1 [L]
if member/centre etc is relative to the root filesystem and not your domain then replace the last line with (just leading slash)
RewriteRule . /member-centre/member-directory/%1 [L]
Do you mean something like:
RedirectMatch 301 ^/members/([^/]+)/? /member-centre/member-directory/$1
This redirects the browser with a permanent redirect.
Try something like this:
RewriteRule ^members/ /member-centre/member-directory/ [L]

How do I fix old links after modifing .htaccess for Joomla installation in a subfolder?

I have my main Joomla installation in a subdirectory. I used to redirect users from www.mysite.com to www.mysite.com/subdir with a 301 so that the live site was entirely dislocated over there.
I don't actually like the fact that all the URL are preceded by the subdirectory /subdir/ (and I also think this is not very good for SEO) so I modified my .htaccess file like this:
RewriteEngine On
RewriteBase /
# Add trailing slash if path does not contain a period or end with a slash
RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule (.*) http://www.mysite.com/$1/ [R=301,L]
#Change http://yoursite.com to http://www.mysite.com (Optional)
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/?(.*)$ http://www.mysite.com/$1 [R=301,L]
#Rewrites http://www.mysite.com/subdir to http://www.mysite.com/
RewriteRule ^(.*)$ subdir/$1 [L]
I also edited the configuration file for Joomla! so that now all the links in the site point (correctly) to www.main.com/theirquery and noto to www.main.com/subdir/theirquery
Now, however, all the old links (that have been posted to other webistes, for example) appears to be broken (404): how can I solve this?
I think I have to redirect (301) them to the new subdirectory-free address, that will be (another time) silently redirected with the htaccess I posted.
But I don't know how to do this!
Thank you in advance!
Can you try setting the $live_site parameter in configuration.php? You need to edit it directly rather than through the backend of Joomla.
You need to add this to your htaccess.
RewriteRule ^subdir/(.*)$ /$1 [R=301,NC,L]
This is not possible since the old links where not physical but stored in a database, so redirecting them wouldn't be possible without passing through the database again.

Redirect .html but not .html?with=options

I'm currently using an .htaccess to get round a problem with a CMS, nothing major and an htaccess fix is tidy enough.
I'm currently using the format...
redirect 301 /pictures.html http://www.domain.com/gallery.html
The problem though this causes is that the CMS uses pictures.html?vars=here to select galleries and so the redirect breaks this part of it. Is there any way I can redirect pictures.html but not when it has variables attached?
For example, add something like
RewriteCond %{QUERY_STRING} ^$
before your rule.
You can redirect everyone except your webserver by detecting the IP address like so:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_HOST} !^123\.45\.67\.89
RewriteRule \.html$ /alternate_page.html [R=301,L]

.htaccess Redirects

How do I set up an .htaccess file to redirect requests to another folder on another domain? I don't want the links to break, I just want them to go elsewhere. Say mysite.com/image.jpg would redirect to site2.com/images/image.jpg.
In .htaccess (in Apache at least):
RewriteEngine On
RewriteBase /
RewriteRule ^images/(.*)$ http://www.yahoo.com/blah/$1 [R=302,L]
Now, what a big surprise, the official documentation has lots of useful examples, including what you are looking for. Yeah, it really sucks that Google is down so often.
You could use mod_rewrite to redirect the request. If you want to redirect everything:
RewriteEngine on
RewriteRule ^ http://other.example.com/images%{REQUEST_URI} [L]
And if you just want to redirect requests with a path that end in .jpg:
RewriteEngine on
RewriteRule \.jpg$ http://other.example.com/images%{REQUEST_URI} [L]
This should help:
Redirect 301 / http://www.example.com/

Resources