Simple Apache Rewrite - mod-rewrite

Folks,
This is easy, but I cannot seem to get this right, any help appreciated.
I if someone goes to a URL
http://test.api.com/somestuff I want it to redirect to
http://test.api.com/en/api/somestuff
However my rewrite rule keeps resulting in a endless redirect - can someone spot the error?
RewriteCond %{REQUEST_URI} !^(/en/api/).*
RewriteRule ^(.*)$ http://%{HTTP_HOST}/en/api/$1 [R,L]

Try this (the slash in the RewriteRule regex is the main difference):
RewriteCond %{REQUEST_URI} !^/en/api/.*
RewriteRule ^/(.*)$ http://%{HTTP_HOST}/en/api/$1 [R,L]

Related

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.

How to rewrite exact url while passing other paths through

I'm new to mod_rewrite, so I know this is probably a simple fix. I want to rewrite www.abc.com to www.xyz.com ONLY. I only want to do this in firefox. I do not want www.abc.com/def to redirect to www.xyz.com. Currently, I have the following:
RewriteCond %{HTTP_HOST} ^www.abc.com$ [NC]
RewriteCond %{HTTP_USER_AGENT} "firefox" [NC]
RewriteRule ^(.*)$ https://www.xyz.com [L,R=302]
I understand that the ^(.*)$ portion is what is allowing everything else to be redirected as well. What should I enter in there that will ONLY redirect www.abc.com?
Thank you!
Change the rule to
RewriteRule ^/?$ https://www.xyz.com [L,R=302]

Mod Rewrite won't redirect subdomains to new URL format

I'm changing all my subdomains to a single domains.
However, in order no to lose all my SEO I need to do some 301 redirections. My problem is that I have about 10.000 subdomains (it's a website about cities and each city is a subdomain) so I need to make a generic rewrite rule in order to make the new URLs (otherwise my htaccess will be too big).
I tried doing it myself but for some reason, it's doing what it wants to (so I guess I'm doing something wrong). Here is my code:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com/b/^(.*)
RewriteRule ^(.*) http://domain.com/city/$1/b/$2 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*) http://domain.com/?multi_city=$1 [R=301,L]
This is what happens with these two rules.
city.domain.com --> domain.com/?multi_city=/
city.domain.com/b/place --> domain.com/?multi_city=/b/place
What am I doing wrong?
Thanks in advance.
So, after many hours, I finally fixed it doing this:
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.(.*)
RewriteCond %{REQUEST_URI} ^/b
RewriteRule ^(.*)$ http://mydomain.%2/city/%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.(.*)
RewriteCond %{REQUEST_URI} ^/event
RewriteRule ^(.*)$ http://mydomain.%2/city/%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com
RewriteRule ^(.*)$ http://mydomain.com/?multi_city=%1 [R=301,L]
This way I can redirect places and events first and if the URL is not in that format then it will go to the different format URL. It's probably not the most efficient solution but it works for me. Hope this helps to someone else.
I think the first RewriteCond it's wrong:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com/b/^(.*)
The '^' symbol says that the string start, it's not part of the group, so I think that you will try:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com/b/(.*)
Maybe, it will be better:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com/b/([a-zA-Z0-9\-]+)
I'm doing it without testing, if it doesn't work I will make tests later and I'll answer you.

mod_rewrite other rule if more parameters

I actually use this mod_rewrite for my System to do it SEO friendly:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
RewriteRule ^(.*)/$ /?c=$1&l=deu
I don't know why it works but after hours on try & error this was the solution for me, now I want to extend it.
I want something like
"if /eng/ is on the end of the url, redirect request to ?c=$1&l=eng"
In every other case it should use the rule posted above.
Can someone help me please?
(Sorry for my bad English)
Try adding this right after the 301 rule
RewriteRule ^(.*)/eng/$ /?c=$1&l=eng [L]

How can I use mod_rewrite to do this

I would like to take a url at my site:
http://mysite.com/jk/drawing
but operationally drop the "jk" dir and have my users see this:
http://mysite.com/drawing
Is this possible? If so can someone give me an example of how it is done?
thanks,
The first thing you need to do is go and change all of your links from looking like this: http://mysite.com/jk/drawing to looking like this: http://mysite.com/drawing. Without doing this, people will still see all the /jk/ URLs everywhere, the only thing you can do about it is to make sure you've changed all your links. Then add these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} mysite.com [NC]
RewriteCond %{REQUEST_URI} !^/jk/
RewriteRule ^(.*)$ /jk/$1 [L]
In order to correct for all the links still pointing to /jk/ that you don't have any control over:
RewriteCond %{HTTP_HOST} mysite.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /jk/([^\ ]*)
RewriteRule ^ /%1 [R=301,L]

Resources