htaccess rewrite change folder and pass query string - .htaccess

I am new at this. How can I rewrite this in .htaccess correctly?
My visitor access:
http://www.example.com/old/xxx/?id=1
But actually source is from:
http://www.example.com/new/(same name with xxx)/1.html

You can try this
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteCond %{REQUEST_URI} ^/old/xxx/?id=%1\.html$
RewriteRule ^(.*)$ http://www.siteurl.org/new/xxx/?id=%1\.html [L,R=301]

Related

change site path by RewriteRule3

i need change the site url path
from
site.net/open.php?cat=22&book=2285
to
site.net/book/2285
i think this code need edit and complite
RewriteEngine On
RewriteRule ^book/([^/]*)$ /books/open.php?cat=22&book=$1 [L]
how to do it by .htaccess and RewriteRule?
i need code work in all categorys also not 22 only
This code should work for you -
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)cat\=22($|&)
RewriteCond %{QUERY_STRING} (^|&)book\=2285($|&)
RewriteRule ^open\.php$ /book/2285? [L,R=301]

.htaccess - Rewrite query string and redirect to directory

I have some old URL's that I want to fix because of a forum migration.
The old URL's look like:
http://www.example.com/forum/topic.asp?TOPIC_ID=666
I want to redirect them to:
http://www.example.com/forum/missions/666
My approach is this, but I'm scratching my head, because it doesn't work at all:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=(.*)$ [NC]
RewriteRule ^/forum$ /forum/missions/%1 [NC,L,R=301]
Assuming there is no .htaccess in `/forum/, you can use this first rule in your root .htaccess:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=([^&]+) [NC]
RewriteRule ^forum/topic\.asp$ /forum/missions/%1? [NC,L,R=302]
If there is a .htaccess in /forum/, then you can use this first rule in your /forum/.htaccess:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=([^&]+) [NC]
RewriteRule ^topic\.asp$ /forum/missions/%1? [NC,L,R=302]
I'd suggest this, but cannot really try from here :)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^forum/topic.asp\?TOPIC_ID=([0-9]+)$ forum/missions/$1 [L]
</IfModule>

htaccess redirect subfolder

I would like to redirect the url http://intranet/trac/paradox/report/6 to http://cobra.woking/trac/paradox/report/6. trac is a subfolder and paradox is a subfolder. report/6 are params that need to be kept and may change.
In my apache doc root i have
#/opt/html/.htaccess
Redirect 301 / http://intranet/intranet
RewriteEngine On
RewriteRule ^trac/paradox/report/6$ http://cobra.woking/trac/paradox/report/6 [L,R]
I have tried the following which does not work
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^intranet$
RewriteRule (.*) http://cobra.woking/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^intranet$
RewriteRule (.*) http://cobra.woking/$1 [R=301,L]
URL i want to change is http://intranet/trac/paradox/ to http://cobra.woking/trac/paradox/. I have placed .htaccess in the /opt/html/trac/paradox/.htaccess
In the htaccess file of your intranet's document root, add this to the top of the file:
RewriteEngine On
RewriteRule ^trac/paradox/report/6$ http://cobra.woking/trac/paradox/report/6 [L,R]

htacces URL rewrite redirection

I have a link: http://testsite.com/api/v2/1
I would like to point my browser to: 1.testsite.com and using redirection in .htaccess link it to this place: http://testsite.com/api/v2/1
How can I do this?
Try this :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.testsite\.com$ [NC]
RewriteRule ^$ http://testsite.com/api/v2/%1 [L]

remove SID from URL and redirect it to a directory with .htaccess

I'd like to remove the ?sid=123456 (can be any number)
redirect: http://www.mydomain.com/directory-a/directory-b/directory-c/?sid=123456
to: http://www.mydomain.com/directory-a/directory-b/directory-c/
According to your question:
"redirect: http://www.mydomain.com/directory-a/directory-b/directory-c/?sid=123456"
"to: http://www.mydomain.com/directory-a/directory-b/directory-c/"
This one way to do it:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} sid=\d+.*
RewriteRule .* %{REQUEST_URI}? [L,R=301]
In short, what the rule does is removing the query string which is what the question is asking for.
Try adding these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^sid=[0-9]+&?(.*)$
RewriteRule ^(.*)$ /$1?%1 [L,R=301]

Resources