I need to redirect through a htaccess file from url1 to url2 with parameters.
Example:
http://www.sensotec.be:8081 => http://activation.sensotec.be:8081
http://www.sensotec.be:8081/lpweb/login.aspx => http://activation.sensotec.be:8081/lpweb/login.aspx
I know there are a lot of cases like this, but I just don't get it.
This rule should work for your case, it will keep everything after the website url, even the port:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SERVER_PORT} ^8081$
RewriteRule ^(.*) http://activation.sensotec.be%{REQUEST_URI} [R=301,NC]
Here's a working example.
Related
I am trying to show different url and redirect users to specific url with .htcaccess when they click on a blog post but to no avail.
Lets say the url is: http://localhost/mySite/article.php?article_title=test-title
then I would like to show it as http://localhost/mySite/article/test-title
This is my current htcaccess file:
#turn on url rewriting
RewriteEngine on
#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#rewrite rule for blog
RewriteRule article/([A-Za-z0-9-]+) /mySite/article.php?article_title=$1
But for some reason it is not redirecting/showing the correct url. I am not getting any errors.
EDIT
Trying to ask my question again and explain it better. Let's say the url is
http://localhost/www.example.com/admin/editUser.php?user_id=126
and I would like to rewrite the url like this:
http://localhost/www.example.com/admin/user/126
then how can I achieve this. I tried using this website to check the modified url but it does not work. Seems like it does not work with any of the accepted answers here in stack at all.
This is my htaccess file atm. It is in the root of www.example.com
#turn on url rewriting
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^user/([0-9]+)/?$ /editUser.php?user_id=$1 [NC,L] # Handle user edit requests
Apache Module mod_rewrite is enabled. Also added an alias. Still no changes in the url. If I try something really basic like this:
# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]
it works fine.
Why is the users redirect not working? What am I doing wrong.
Try it like this for your rule for article url in mysite directory.
RewriteRule ^article/([A-Za-z0-9-]+)$ article.php?article_title=$1 [QSA,NC,L]
you need to mention start ^ and end $ of string.
I need to redirect all pages with https:
https://shop.test.com/
https://shop.test.com/several_parameters
To http://www.test.com.
But we should not redirect a page like
https://shop.test.com/checkout/onepage
It sounds like there are certain URLs that you want to allow as https on shop.test.com. For each of these rules, add a RewriteCond like the following (replacing {URL HERE} with the URL you want to allow):
RewriteCond %{REQUEST_URI} !^{URL HERE}
Here is the entirety of the solution to deal with the cases that you specified above:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^shop.test.com$
RewriteCond %{REQUEST_URI} !^/checkout/onepage
RewriteRule .* http://www.test.com/$1 [L,R=301]
Caution!
It's worth noting that if you don't whitelist a URL that is listed as secure by Magento that you will create an infinite redirect loop. To help make sure that you whitelist every possible secure URL, you can grep the codebase for <secure_url> and whitelist each of those endpoints.
Hi i would like ask you about .htacess 301 redirect:
I have many dynamic urls with paths for example like this:
http://www.domain.com/post1.html
http://www.domain.com/post2.html
and i need to redirect it to the same url but with little bit different domain (without WWW):
http://domain.com/post1.html
http://domain.com/post2.html
I have done like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com
RewriteRule (.*) http://domain.com/$1 [R=301,L]
Redirect works, but my normal urls becomes urls with ID's like:
http://domain.com/index.php?id=931
not like it should be:
http://domain.com/post1.html
Any ideas or helo are appreciated.
Thank you.
You have some other rewrite rules happening here obviously, but with what you've given us, you could try adding this before your rules in order to get it to stop processing an URL which has been rewritten as such:
RewriteCond %{QUERY_STRING} id=[0-9]+
RewriteRule ^index.php - [L]
Ok, now I am lost.
I am trying to do a simple .htaccess redirect of subdomains to a specific folder on the server, meaning all
subdomain.mywebsite.com
will go to
www.mywebsite.com/s_subdomain
But for some reasons this doesn't work.
I have tried a lot of settings in .htaccess but for no good. Now in my .htaccess I have:
RewriteEngine on
Options +FollowSymLinks
Options +SymlinksIfOwnerMatch
RewriteCond %{HTTP_HOST} !^(www|ftp|mail)\.mywebsite\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.mywebsite\.com
RewriteRule (.*) /s_%1/$1 [L]
Are there any other settings, or is somethig I have missed?
PS. - I don't have access to http.conf. I have to do it using only .htaccess
Thanks!
This is just a "plain" rewrite (the browser won't see it). To redirect, add the R flag to your RewriteRule.
RewriteRule (.*) /s_%1/$1 [L,R]
The rest seems right, although I haven't tested it. For debugging you could consider RewriteLog, see http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritelog
So, neither solution does work? Try something simple then.
RewriteEngine on
RewriteCond ${SERVER_NAME} ^(subdomain)\.yoursite\.com$ [nc]
RewriteRule ^(.*)$ http://www.yoursite.com/s_%1/$1 [L,R]
To test if your subdomain is handled correctly, create random.html file, place it where it should be read from, and try opening it via http://subdomain.yoursite.com/random.html. Then you can try some stuff like:
RewriteRule ^random.html - [F]
...and if that blocks access to file, try prepending
RewriteCond ${SERVER_NAME} ^subdomain\.yoursite\.com$ [nc]
to previous rule, to block access to that file, to make sure that rewrite engine is actually hitting your rules. That would target only desired subdomain (www.yoursite.com/random.html should work, but access via subdomain shouldn't).
Then if those rules work, it's just a matter of adding more stuff and see when it stops working.
RewriteRules are a bitch.
The following should work:
.htaccess:
RewriteCond ${SERVER_NAME} !^(www|ftp|mail)\.example\.com$
RewriteCond ${SERVER_NAME} !^([^.]+)\.example\.com$
RewriteRule .* redirect.php?to=%1
redirect.php
<?php
$desired_server_name = 'http://example.com';
$subdir = 's_' . $_GET['to'];
$url = $desired_server_name . '/' . $to . $_SERVER['REQUEST_URI'];
// Permanent redirects
header('HTTP/1.1 301 Moved Permanently');
// Or simple redirects:
header('HTTP/1.1 302 Found');
header('Location: '.$url);
?>
Works on my server (debian 4/apache 2).
Bonus: do not EVER use HTTP_HOST! See the following request:
HTTP/1.1 GET /foo/bar.php
Host: www.host.tld"><script>alert(/Hello/)</script
Connection: close
If you use $_SERVER['HTTP_HOST'] in your .php scripts to construct links or .htaccess rules for that matter and "www.host.tld" is the virtual-host or the only host configured for Apache, the XSS in the HTTP request header will be passed down unescaped.
We have a similar thing working on our Virtual Machines, where we redirect anything.usertld to a folder for that domain, that was in httpd.conf, tried in in the .htaccess and like yours it didn't work.
Tweaking it, this works for me (my VM occupies a tld called benb, but changing it to your domain should be fine):
RewriteCond %{HTTP_HOST} !^www\.benb
RewriteCond %{HTTP_HOST} ^(.*)\.benb
RewriteCond %{REQUEST_URI} !^/{0,1}s_
RewriteRule ^(.*)$ s_%1/$1 [L]
Also this captures all the text before the domain.. you should be able to change:
RewriteCond %{HTTP_HOST} ^(.*)\.benb
to
RewriteCond %{HTTP_HOST} ^([^.]+)\.benb
to handle just 1 level of subdomain. Also your other part about (www|ftp|mail) would work fine too.
I am new to mod_rewrite. I am trying to forward a URL to another one, but I cannot get it to work.
Say I want to forward this URL:
/cansas.php?m=2&id=2-0-0-0&sid=cansas to
/cansas-is-good-for-you and let the header respond with a 301, or just update the URL through [R].
I have this in my .htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^cansas.php?m=2&id=2-0-0-0&sid=cansas$ cansas-is-good-for-you [NC,R=301]
I figured I could just do a simple forwarding, but somewhere along the way it stops working. If I cut out the ?m=2&id= etc, it forwards just the cansas part to the new part so it looks like this: cansas-is-good-for-you?m=2&id=2-0-0-0&sid=cansas.
How can I forward it when I have several dynamic parameters in the URL string? Example on pages I need to forward:
/cansas.php?m=2&id=2-0-0-0&sid=cansas
/cansas.php?m=2&id=2-1-0-0&sid=cansas
/cansas.php?m=2&id=2-2-0-0&sid=cansas
Any help would be greatly appreciated :)
Maybe it isn't possible to do it this way? The way I have set it up at the moment is that I want to use new URLs called /cansas-is-good-for-you which reads from the source /cansas.php?m=2&id=2-0-0-0&sid=cansas, but the URL shown in the browser should be: /cansas-is-good-for-you. I need to forward that old cansas.php? URL to the new URL :)
You need to check the query of a URL with the RewriteCond directive as the RewriteRule directive only handles the URL path:
RewriteCond %{QUERY_STRING} ^m=2&id=2-0-0-0&sid=cansas$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]
If you want to check for just one parameter, use this:
RewriteCond %{QUERY_STRING} ^([^&]*&)*sid=cansas(&.*)?$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]
And to do this just for initial requests, you need to check the request line:
RewriteCond %{THE_REQUEST} ^GET\ /cansas\.php
RewriteCond %{QUERY_STRING} ^([^&]*&)*sid=cansas(&.*)?$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]