I want to redirect permanent from one domain to another, such as:
http://www.example.de/page.html?cid=00340119050014953926&pc=70000
to this:
http://www.secondexample.com/page.html?cid=00340119050014953926&pc=70000
So it's a different domain with the same parameters.
How this can be done?
This can also be done with an htaccess redirect.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*) http://www.secondexample.com%{REQUEST_URI} [R=302,NC]
Change your index file in your old domain to this:
<html>
<?php
header('Location: http://www.secondexample.com/page.html?cid=00340119050014953926&pc=70000');
exit;
?>
You can simply use a header redirect.
If you need to keep the old get data you can include simple parsing in yout old do,ain of the get data and use it in the redirect.
See PHP header for more info.
If the goal is to always redirect the page.html page to the new domain regardless of what's in the query string, you can do this in htaccess with something similar to:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^page.html(.*)$ http://www.secondexample.de/page.html$1 [R=301,L]
</IfModule>
If these domains are on a shared server and actually point to the same files as one another, you'll need to add a condition above the RewriteRule in the above:
RewriteCond %{HTTP_HOST} ^example.de$
can be done using .htaccess file
RewriteCond %QUERY_STRING ^(.+)$
RewriteRule page\.html http://www.secondexample.com/?%1 [R=301]
Related
I'm trying to use GET to capture member IDs in a URL without using the ?name=variable. For instance, instead of using mydomain.com/folder?id=1234 I would like to use mydomain.com/folder/1234 and pick up the id "1234" in the code so I can pass it to another URL.
This page simply redirects by using: <iframe src="http://redirect_domain.com/folder/index.php" />
I have no control over the server I'm redirecting to, but need the variable passed to it. I can append the redirect URL with ?id=1234 ie <iframe src="http://redirect_domain.com/folder/index.php?id=1234" />
Their code will pick up the id by using <?php echo $_GET["id"]; ?>
Thanks for your help.
You'll want to use .htaccess for this. Create a file called .htaccess and put it in the same folder mydomain.com is referencing. Put something like the following in there
RewriteEngine On
RewriteRule ^folder/(.*)$ folder?id=$1 [L]
Note: mod_rewrite must be enabled on apache, but it usually is.
Update: I've updated the .htaccess to reflect a local URL since you've made your desired functionality more clear. Using this .htaccess in conjunction with <iframe src="http://redirect_domain.com/folder/index.php?id=<?=$_GET['id']?>" /> should do the trick.
Create a .htaccess file and use something like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I feel the need to write my own answer down. The following two lines in your .htaccess should do exactly what you are asking for:
RewriteEngine On
RewriteRule ^folder/(.*)$ redirect_domain.com/folder/index.php?id=$1 [L]
According to the rewrite rule test site http://martinmelin.se/rewrite-rule-tester/ , when I use an input of folder/1234, it is redirected to redirect_domain.com/folder/index.php?id=1234 which is exactly what you asked for. There is no need for the <iframe... or any actual files in the /folder/ other than the .htaccess ...
I'm not the best at this but I have to do a redirect
domain1.tld/subfolder/seo/based/uri
and i need to redirect to
subd.domain2.tld/seo/based/uri
is that possible?
EDIT:
Forgot to mention, these all dynamic requests. Like:
domain1.tld/forums/forum/2-network-announcements/topic-name-with-id
This can be done in .htaccess or PHP, but I find it's easier in PHP.
To do it this way, the PHP file for the first URL should contain:
<?php
header("Location: http://subd.domain2.tld/seo/based/uri");
?>
More info here: http://php.net/manual/en/function.header.php
Redirecting from .htaccess is immediate. Meaning, if you redirect from .htaccess, no code will be run at domain1.tld/subfolder/seo/based/uri. If that's what you want, then add these lines to your .htaccess:
RewriteEngine On
RewriteRule ^domain1.tld/subfolder/(.*)$ subd.domain2.tld/$1
If you do want to run some code on domain1.tld/subfolder/seo/based/uri, then go read #tomtheman5's answer, because that will be closer to what you want.
In the htaccess file or the vhost for domain1.tld, add:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.tld$ [NC]
RewriteRule ^/?subfolder/(.*) http://subd.domain2.tld/$1 [L,R=301]
You could also use mod_alias if the 2 hosts have different document roots:
Redirect 301 /subfolder http://subd.domain2.tld
I am trying to write rewriterule for php site.
My URL is like http://example.com/attorneys?pid=69
I write in .htacess as below:
RewriteEngine On
RewriteRule ^attorneys/([0-9]+)/?$ attorneys&pid=$1 [NC,L]
Both the link example.com/attorneys?pid=69 and example.com/attorneys/69 works.
How can I make the browser know that if it get the first link it have to show the second one in browser.
RewriteRule ^attorneys/([0-9]+)/?$ attorneys&pid=$1 [NC,L,R=302]
So you want to redirect http://xyz.com/attorneys?pid=69 to http://xyz.com/attorneys/69? Another rule after(!) the first rule should do the trick:
RewriteEngine On
RewriteRule ^attorneys/([0-9]+)/?$ attorneys&pid=$1 [NC,L]
RewriteRule ^attorneys&pid=([0-9]+)$ attorneys/$1 [NC,L,R=301]
Because the first rule is marked with the L flag, the second won't be executed if the first matches. (See the documentation of mod_rewrite flags here.)
First, I need to say you there is no need to do that. Anyway, I was forced to do it in the past for a SEO-maniac client. I tell you, that's not an elegant solution!
On top of the attorneys PHP page (I don't know if it's a directory with index.php or not, but you know it) add this code:
// Get request script
$request = preg_split('/[\\/?&]/', $_SERVER['REQUEST_URI']);
$request_script = $request[1];
// Check old URL
if ($request_script == 'attorneys') {
// Redirect
header('Location: /attorneys/' . $_GET['id'];
exit();
}
Maybe it's not exactly like this for your case, but I hope you get the mechanism.
This appears to be a simple ask. Try this code in your .htaccess file under DOCUMENT_ROOT (and comment out your existing code):
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteOptions MaxRedirects=5
RewriteRule ^(attorneys)/([^/]+)/?$ $1?pid=$2 [NC,L]
RewriteCond %{QUERY_STRING} ^pid=(.*)$ [NC]
RewriteRule ^(attorneys)/?$ /$1/%1? [NC,L,R=301]
I want to have a PHP file catch and manage what's going to happen when users visit:
http://profiles.mywebsite.com/sometext
sometext is varying.
E.g. It can be someuser it can be john, etc. then I want a PHP file to handle requests from that structure.
My main goal is to have that certain PHP file to redirect my site users to their corresponding profiles but their profiles are different from that URL structure. I'm aiming for giving my users a sort of easy-to-remember profile URLs.
Thanks to those who'd answer!
Either in Apache configuration files [VirtualHost or Directory directives], or in .htaccess file put following line:
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L,NC,QSA]
</IfModule>
It will silently redirect all incoming requests that do not correspond to valid filename or directory (RewriteCond's in the code above make sure of that), to index.php file. Additionally, as you see, MultiViews option also needs to be disabled for redirection to work - it generally conflicts with these two RewriteCond's I put there.
Inside index.php you can access the REQUEST_URI data via $_SERVER['REQUEST_URI'] variable. You shouldn't pass any URIs via GET, as it may pollute your Query-String data in an undesired way, since [QSA] parameter in our RewriteRule is active.
You should use a rewrite rule..
In apache (.htaccess), something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>
Then in your index.php you can read $_GET['url'] in your php code.
You can use a .htaccess file (http://httpd.apache.org/docs/1.3/howto/htaccess.html) to rewrite your url to something like profiles.websites.com/index.php?page=sometext . Then you can do what you want with sometext in index.php.
An obvious way to do this would be via the 404 errorDocument - saves all that messing about with mod_rewrite.
If you have not heard about MVC, its time you hear it, start with CodeIgniter, its simplest and is quite fast, use default controller and you can have URLs like
domain.com/usernam/profiledomain.com/usernam/profile/editdomain.com/usernam/inboxdomain.com/usernam/inbox/read/messageid Or use .htaccess wisely
i am working for a site,which is in php.....i want to rewrite url
e.g www.3idiots.co.in/stories.php?id=17
if i want to rewrite it as
www.3idiots.co.in/stories/17.html
can any one tell me the code for this to write in .htaccess file.?
I'm assuming you're using Apache with mod_rewrite. Something like
RewriteEngine On
RewriteRule ^/stories/([0-9]+)\.html /stories.php?id=$1
should do the trick. Of course you'll need to make sure that RewriteRule is allowed in that directory. See this wiki page for more information.
mod_rewrite can only rewrite/redirect requested URIs and not those that are in your HTML documents. So you should first make sure, that your PHP application is printing the correct URIs, so /stories/17.html instead of /stories.php?id=17.
After that, you can use the rule suggested by José Basilio:
RewriteRule ^stories/([0-9]+)\.html$ stories.php?id=$1
Though redirecting requests of /stories.php?id=17 externally to /stories/17.html and then internally back to /stories.php?id=17 is possible, it’s not good practice as that would result in twice as many requests. But here’s the rule for that:
RewriteCond %{THE_REQUEST} ^GET\ /stories\.php[?\s]
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([0-9]+)&*([^&].*)?$
RewriteRule ^stories\.php$ /stories/%3.html?%1%4 [L,R=301]