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]
Related
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]
What I have is a url like this:
domain.com/index.php?chapter=chapter-name
and a url link this:
domain.com/index.php?marker=marker-name
What I want is:
domain.com/#chapter-name
and this:
domain.com/#chapter-name/marker-name
How can I do this with .htaccess?
I now have the following:
RewriteEngine On
RewriteRule ^(.*)/$ http://%{HTTP_HOST}/$1
RewriteRule ^([a-zA-Z0-9_-]+)?$ index.php?chapter=$1
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?chapter=$1
Thanks!
To get the chapter working, try this rule:
RewriteRule ^index.php\?chapter=([a-zA-Z0-9_-]+)$ http://www.%{HTTP_HOST}/#$1
Your second needs is a arbitrary... how does the marker "know" which chapter... Maybe you meant this on your second request:
domain.com/index.php?chapter=chapter-name&marker=marker-name
You can't. Basically # is a client thing, and never sent to the server. .htaccess therefore will not work.
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 have this url:
details
the url becomes in addressbar:
http://web228.sydney.webhoster.ag/soputnik/drive_to_london/?procid=12
but I want to process the logic in details.php.
How can I process the data in details.php in background and keep the first url in the address?
I tried this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^drive_to_(.*)$ http://web228.sydney.webhoster.ag/soputnik/details.php [R=301,L]
but it is not working, error is: NOT FOUND
please help
Substitute your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^test/drive_to_(.*?)/?$ /test/details.php [L,NC]
If you don't want the URL to change, then it's not a redirect, just a rewrite.
Change:
RewriteRule ^/test/drive_to_(.*)$ /test/details.php [R=301,L]
To something like:
RewriteRule ^/test/drive_to_(.*)$ /test/details.php?city=$1 [L]
dont forget to pass any url params to details.php.
Rewriterule ^/drive_for_(.*)/\?(.*)$ http://domain.de/test/details.php/?$1 [R=301,L]
it seems relevant that you want to pass the city name as well, otherwise that context gets lost in the rewrite. If that is another url param you could pass it through to details.php as such
Rewriterule ^/drive_for_(.*)/\?(.*)$ http://domain.de/test/details.php/?$2&city=$1 [R=301,L]
I'm lost here. I'm using this script to give users the opportunity to enter their username lijke this:domain/username
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ userpage.php?user=$1 [NC,L]
</IfModule>
This works fine. However, every user has pages I must link to: Video, Music, Images etc...
So I need something like:
domain/username/video
In php code it must be something like:
user.php?user=test&page=video
And one other question: What is the preferable way to link in this situation?
userpage.php?user=test&page=video
or
/test/video
And finally: Is it possible to deny the possibility to enter the url:
domain/userpage.php?user=test&page=video? Instead just always show: domain/test/video
Thanks in advance
I'm not 100% sure what you're asking? Do you need to change the rewrite rule to match the URL site.com/moonwalker/videos? You could try this:
RewriteRule ^([^/]+)/(images|videos|music)/?$ userpage.php?user=$1&page=$2 [NC,L]
Update
Just a quick note on the domain/member/videos URL structure. That could end up causing you problems in the future. For instance what if you decide to have a single page that shows all member videos? You'd probably want to URL to look something like site.com/members/videos. That's a problem, because the rewrite rule will also match that, but "members" isn't a member username.
I would probably structure my member page URLs like site.com/user/moonwalker/videos so it doesn't clash with future rewrite rules. You would change the above rewrite rule to this:
RewriteRule ^user/([^/]+)/(images|videos|music)/?$ userpage.php?user=$1&page=$2 [NC,L]
Then later on you can add a rewrite rule like:
RewriteRule ^members/(images|videos|music)/?$ allusers.php?page=$1 [NC,L]
To show all member videos.
Yes, it is possible by looking at the request line:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /userpage\.php[?\ ]
RewriteRule ^userpage\.php$ - [F]
This is necessary as the URL path could already be rewritten by another rule and thus using just RewriteRule would match those already rewritten requests too.