htaccess to redirect url with params - .htaccess

I have to redirect a url to another url with all its parameters using htaccess.
My incoming url will be something like this:
www.mydomain.com/book-it.jsp?id=3&var1=values&var2=value2...
and I want to get it in PHP (book-it.php) as something like this (with all its parameters):
www.mydomain.com/book-it.php?id=3&var1=values&var2=value2...
I was using JSP and from now on moving to PHP and we need to use same URL since this url is already published with my application and I can't change that now. I need to get that url and parameters to another file.

You have 2 main approaches:
1) Using Redirect directive:
Redirect 301 /book-it.jsp http://www.mydomain.com/book-it.php
2) Using mod_rewrite module (this needs to be placed in .htaccess in website root folder. If placed in Apache config file (inside <VirtualHost>, for example) the rules need to be modified slightly):
RewriteEngine On
RewriteBase /
RewriteRule ^book-it\.jsp$ http://www.mydomain.com/book-it.php [QSA,NC,R=301,L]
Both of them will preserve query string.

Related

Redirect one URL to another on same domain with htaccess

I am currently building a new website. The old website has different url's than the new one. Now i want to redirect, using htaccess.
Before going live, i want to test my rules locally.
The old website uses url's of this format:
www.domain.de/?content=whatevercontent
The new website uses url's of this format:
www.domain.de/index.php?content=differentcontentname
Which are rewritten (in the htaccess file, using several RewriteRules) to this format:
www.domain.de/nicecontentname
I tried Redirects like this:
Redirect http://domainfolder/?content=whatevercontent http://10.3.10.69/domainfolder/nicecontentname
This does not work.
After going live it should work like this:
Redirect http://www.domain.de/?content=whatevercontent http://www.domain.de/index.php?content=differentcontentname
..and then be rewritten to the nice url.
My Redirect-Rules just won't apply, i tried it in all combinations i could think of, with or without http, with or without the containing folder, using the already rewritten url or the actual one, etc..
Any ideas on this issue?
You can not use Redirect directive to manipulte querystrings. Here is a mod-rewrite example that works for QueryStrings :
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/\?content=this
RewriteRule ^$ /index.php?content=that [L,R]
This will temporary redirect /?content=this to /index.php?content=that
To make the redirect permanent (browser cacheable) change R to R=301.

.htaccess rewrite with subdirectory

I want to rewrite (not redirect) all url's of my site to a sub-directory of my site. For example:
http://example.com/example
would load the following:
http://example.com/public/example
Though, requesting http://example.com/public should not load the contents from public/public but from /.
Answers I've found on SO either do the above with redirect (which I don't want) or doesn't account for the special case above.
EDIT: further clarification:
I want every request on my site to go load under the public folder, but without being visible to the visitor. So requesting http://example.com/index.php will load the file from http://example.com/public/index.php. The url in the browser remains unchanged for the user.
Try the following rule :
RewriteEngine on
RewriteRule ^((?!public).+)$ /public/$1 [NC,L]
This will rewrite all requests from root to /public dir.

How do I redirect a web URL using .htaccess

I want to redirect users who enter this website URL:
http://www.myWebsite.com/bananas
to:
http://www.myWebsite.com/fruits/bananas
I cant test it because I'm sending this to somebody.
I have these but I don't know for sure which one works:
RedirectMatch 301 http://www.myWebsite.com/bananas(.*) http://www.myWebsite.com/food/bananas $1
Options +FollowSymlinks
RewriteEngine on
rewriterule ^bananas(.*)$ http://www.myWebsite.com/food/bananas $1 [r=301,nc]
Please specify if you want to redirect or rewrite. The rules you are using serve different purposes and you used both in your example.
Redirect: Actually load a different site when entering the url (end up at url and content of /fruits/bananas)
Rewrite: Url stays the same but server provides rewritten content (url stays at /bananas, but show /fruits/bananas content)
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Also it is not clear if you only want one single directory to be redirected or all files that are contained in that directory.
Checkout this as a guide: http://www.htaccessredirect.net/
I believe you are looking for
Redirect 301 /bananas http://www.myWebsite.com/fruits/bananas
The HTTP 301 stands for Moved Permanently.
I haven't tested it, though.

Redirect static html URL to an .htaccess rewritten URL

I have fully static website, now I want to change this website to dynamic. But problem is that static website having good traffic and I do not want to lose that traffic. For the dynamic website I already rewrite the URL using htaccess for SEO reasons. I want to redirect the static url to the rewritten url (which was written by my .htaccess).
(A) Static URL :
www.website.com/examples/java/datatype/boolean/printbooleanvalue.html
(B) Original Dynamic URL:
www.website.com/examples.php?language=?java&category=data-type&subcategory=boolean&exampleurl=print-boolean-value
(C) Rewritted Dynamic URL :
www.website.com/examples/java/data-types/boolean/print-boolean-value
So I want to redirect URL(A) to URL(C). Is there any way to do this ?
You can hardcode the mod rewrite to load the php file when the html file is called.
The SEO friendly url that you have created in your .htaccess is redundant. Instead of the SEO friendly URL, use the same url as your original site.
For example in your .htaccess file write the rule like
RewriteEngine On
RewriteBase /
RewriteRule examples/java/datatype/boolean/printbooleanvalue.html$ /examples.php?language=?java&category=data-type&subcategory=boolean&exampleurl=print-boolean-value [L]
This will load the php file whenever the original html file is requested and it will use the same url as the older one.
Hope that helps.
Your .htaccess code:
Redirect /examples/java/datatype/boolean/printbooleanvalue.html http://www.website.com/examples/java/data-types/boolean/print-boolean-value
In any case you run into the need to redirect:
Redirect /olddirectory/oldfile.html http://yoursite.com/newdirectory/newfile.html

Replace the filename in the URI using htaccess

In my website, the main PHP script is named index.php. All URIs look similar to this:
index.php?section=FrontPage&l=ES_LA&view=multiple
In the htaccess file and using the mod_rewrite extension, I managed to remove the .php PHP extension, so the URI looks like this:
index?section=FrontPage&l=ES_LA&view=multiple
What I want to do now is to replace index.php with example in the path.
I've tried using:
RewriteRule ^example$ index.php
But when I try accessing example?section=FrontPage&l=ES_LA&view=multiple the server returns a 404 Not Found status code.
How can I fix this?

Resources