double slash apache configuration - mod-rewrite

i'm deploying a ror application and now i have to rewrite the url (in apache) to
add a prefix www to the url
add / to the end of the url
So i took the following approach:
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteRule ^(.*)$ http://www.foo.com/$1 [R=301,L]
The problem is that it is appending two trailing slash to my url
So for example a resource /question/ask are becoming:
http://foo.com//question/ask
I tried to add the following Rule before all my Rewrite rules to try to remove the double //:
RewriteCond %{REQUEST_URI} ^//
RewriteRule ([^/]*)/+(.*) http://www.foo.com/$1/$2 [R=301,L]
but it didnt work.. any idea to rip off all extras "//" added to the url?

The $1 will include a / at the beginning. You probably want
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteRule ^(.*)$ http://www.foo.com$1 [R=301,L]

Related

Rewrite example.txt to another based on the domain

Im using a multi-install for my webpage with two different domains and i need for each domain a unique robots.txt
like
https://www.domain1.tdl/robots.txt should use the https://www.domain1.tdl/robots_domain1.txt
and
https://www.domain2.tdl/robots.txt should use the https://www.domain2.tdl/robots_domain2.txt
Check this rewrites in .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.tdl [NC]
RewriteCond %{REQUEST_URI} ^\/robots\.txt$
RewriteRule (.*) https://www.domain1.tdl/robots_domain1.txt [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.tdl [NC]
RewriteCond %{REQUEST_URI} ^\/robots\.txt$
RewriteRule (.*) https://www.domain2.tdl/robots_domain2.txt [L]

redirect all pages including sub pages to new domain

I tried this:
RedirectMatch 301 (.*) http://olddomain.com$1
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^olddomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
But all subpages are not redirected.
Try this,
RewriteEngine On
# Take care of www.old.com.au
RewriteCond %{HTTP_HOST} ^www.old.com.au$ [NC]
RewriteRule ^(.*)$ http://www.new.com/$1 [L,R=301]
RewriteCond %{QUERY_STRING} ^attachment_id=([0-9]*)$ [NC]
RewriteRule ^$ http://www.new.com/? [R=301,NE,NC,L]
It's simple, I was just using this to do some special rewriting for my own, here is your code:
Put this inside your /www/.htaccess file:
RewriteEngine on
// Rules to redirect to another domain
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.net/$1 [L,R=301,NC]
Check http://www.inmotionhosting.com/support/website/redirects/setting-up-a-301-permanent-redirect-via-htaccess, for 3 more ways to make a redirection.
Are you setting directive AllowOverride All in your Apache config?
Is the mod_rewrite module working?

RewriteCond %{REQUEST_URI} not working

I am trying to redirect all requests coming in to the web server as http://portal.company.com/legacy to http://portal.company.com/wps/portal/public/legacy/legacyportlet with the following rule, but it is not working as expected.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^portal\.company\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/legacy$ [NC]
RewriteRule ^(.*)$ /wps/portal/public/legacy/legacyportlet$1 [NC,L,PT]
I have also tried
RewriteCond %{HTTP_HOST} ^portal\.company\.com$ [NC]
RewriteRule ^/legacy /wps/portal/public/legacy/legacyportlet [NC,L,PT]
Any help would be greatly appreciated!
Thanks
It doesn't look like your source or target URLs change in any way, so possibly you're better off using Apache's basic Redirect directive which just redirects one URL to another.
Use this rule:
RewriteCond %{HTTP_HOST} ^portal\.company\.com$ [NC]
RewriteRule ^legacy/?$ /wps/portal/public/legacy/legacyportlet [NC,L]
Remember that in .htaccess RewriteRule doesn't match leading slash of URI.

RewriteCond Query String .htaccess

EDIT: I must add something.First of all i want to change url display because of SEO.If i use www for reach my website there is no problem, second link is appears and everything is ok.
But if i remove "www" from link, it changing to first url and i doesn't want that.
I want to change
http://www.mysite.com/index.php?route=epson-claria-uyumlu-yazici-kartus-dolum-murekkebi-500g.html
to
http://www.mysite.com/epson-claria-uyumlu-yazici-kartus-dolum-murekkebi-500g.html
how I can do it?
I tried
RewriteCond %{QUERY_STRING} ^_route_=(.*)$
RewriteRule ^index\.php$ /%1 [R=301,L]
but it is not working.
My .htaccess is
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
RewriteCond %{QUERY_STRING} ^route=common/home$
RewriteRule ^index\.php$ http://www.mysite.com? [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
Maybe this is what you're looking for:
RewriteRule ^(.*)$ index.php?route=$1 [L]
If you have to visually change the address bar, leave the RewriteRule in place as I described above, and put this in your index.php before any output:
if(isset($_REQUEST['route']))
{
header('Location: '.urlencode($_REQUEST['route']));
}
Initial note: I'm not an Apache guru so don't rely blindly on my answer.
I would first redirect to www. if required
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{SERVER_NAME}/$1 [R=301,L]
Then make it go to the page indicated by the _route_ query variable
# if it is the index page...
RewriteCond %{REQUEST_URI} ^/(index\..+)?$ [NC]
# and if the query string starts with _route_=
RewriteCond %{QUERY_STRING} ^_route_=(.*)$
# redirect
RewriteRule ^(.*)$ http://%{SERVER_NAME}/%1? [R=301,L]
Server variable SERVER_NAME in last line might need to be changed with HTTP_HOST.

removing the forward slash from this re-written url

I know this may sound silly little bit, but there is no other way to resolve it. I have an issue with the forward slash /. I want to remove it from the url because it is kind of making some confusion to the browsers giving trusted or un-trusted urls.
This is the code for the main domain:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^ /user [R=301,L]
and this is the output of the above code: https://www.mydomain.com/user/
but I want the url redirect to this:
https://www.mydomain.com/user
I also want to remove the forward slash from this url :
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(?:\.\w+|/)$
RewriteRule (.*) /$1/ [R,L]
RewriteCond %{REQUEST_URI} ^(/end)
RewriteRule ^(.*)$ /top/right/left/bottom/$1 [L]
RewriteCond %{REQUEST_URI} ^/top/right/left/bottom
RewriteRule top/right/left/bottom/(end)(/(./*))?$ $1$2 [R,L]
and this is the output of the above code: https://www.mydomain.com/end/
but I want the url redirect to this:
https://www.mydomain.com/end
Last question: why do I have to write www. in my own in some browser like FF or chrome so the redirect make itself to the required url?
All comments are appreciated
Looks like the above rewrites rules are written by me. So optimizing it for URL to not end with a / (https://www.mydomain.com/end)
Replace the whole(from RewriteCond %{ENV:REDIRECT_STATUS} 200 to RewriteRule top/right/left/bottom/(end)(/(./*))?$ $1$2 [R,L]) thing by this:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteRule ^(end)(?:/(.*))? /top/right/left/bottom/$1/$2 [L]
RewriteRule ^top/right/left/bottom/(end)(/.*)?$ $1$2 [R,L]
Remember though if you access with a forward slash like this: https://www.mydomain.com/end/ the forward slash will not be removed.
If you want the / to be removed: Replace the last 2 rules by these:
RewriteRule ^(end)(?:/(.+))? /top/right/left/bottom/$1/$2 [L]
RewriteRule ^(end)/$ $1 [R,L]
RewriteRule ^top/right/left/bottom/(end)(/.*)?$ $1$2 [R,L]
Store the url in a variable and perform rtrim to remove trailing slash
$redirectURL = rtrim($redirectURL, "/");

Resources