mod_rewrite other rule if more parameters - mod-rewrite

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]

Related

Rewrite rule match ALL not working

So I want any visitor to a site: https://domain.example.com/[IP Address] to actually get the contents of: https://domain.example.com/api-index?ip=[IP Address].
I thought this one is easy:
RewriteRule ^(.*)$ api-index.php?ip=$1 [L]
It is actually loading content from api-index.php, but not the ?ip=$1 part.
Am I missing something?
Thanks!
This looks like it should work fine. You can test it by temporarily adding R=302 to your RewriteRule flags as then you will see the new address in your browser.
If I may suggest, you may find it easier to use FallBackResrouce /api-index.php and parse the PATHINFO
I solved this by following this question.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* api-index.php?ip=$0 [PT,L]

Mod rewrite for query string

Mod Rewrite noob so pardon my ignorance but all I am trying to do is a simple query string removal
from: http://yourwebsite.com/x?section=y
to: http://yourwebsite.com/x/y
I am adding my mod rewrite rules in my .htaccess like this:
ErrorDocument 404 /404
Options +MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{QUERY_STRING} ^section=(.*)$ [NC]
RewriteRule ^(.*)$ /$1/%1? [R=301,L,NE]
The problem is that on visiting:
http://yourwebsite.com/x?section=y
My rule writes it back as:
http://yourwebsite.com/x.php/y
That .php in the pretty url is pretty darn ugly and I am struggling to get rid of it.
What is wrong in my mod rewrite rule?
Most likely you are looking for something like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)\/(.+)$ /$1.php?section=$2 [L]
RewriteCond %{QUERY_STRING} ^section=(.*)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1/%1 [R=301,L,NE]
It makes two rewrites:
a request x/y is internally rewritten to x.php?section=y
a request to x?section=y is redirected to x/y
Note that one is an internal rewrite, whilst the other redirects the browser to show a less 'ugly' url.
One hint: in case you can use the logging feature of apaches rewriting module (RewriteLog and RewriteLogLevel) this will offer you a wealth of detailed information on what is actually going on inside the rewrite process.

Mod rewrite redirection to another domain if file not exist

What I'm trying to do:
I need to redirect a request to a file to another domain if the file not exists. For example:
http://www.mydomain.com/js/foo.js
redirects to (if not exists)
http://www.myanotherdomain.com/js/foo.js
What I do:
I wrote the next lines at the end the htaccess, but they redirect ALL!
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^(.*)$ http://www.myanotherdomain.com/$1 [L,NC]
Before these lines, I have a lot of lines like this (I'm using MVC (Model, View,Controller)):
RewriteRule ^car/brand/?$ ?controller=Car&action=viewBrand [L,NC]
What happens:
It works wells with non existing files, but seems to be imcompatible with the MVC rules. These rules have to match and then stop evaluating rules because de "L" flag. But it seems to continue evaluation of the rules and finally evaluates the redirect rule. The result is this:
http://www.mydomain.com/car/brand/
goes to
http://www.myanotherdomain.com/?controller=Car&action=viewBrand
Please can anyone help me?
Thank you very much,
Jonathan
Try this:
RewriteCond %{REQUEST_FILE} !-f
RewriteRule ^(.*)$ http://www.myanotherdomain.com/$1 [QSA,R,L]
See also: mod rewrite directory if file/folder not found
Try placing these rules after your MVC rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.(js|png|jpe?g|gif|css|html?)$ http://www.myanotherdomain.com/$1.$2 [L,R,NC]
If you want all requests, and not just static content like scripts and images then change the RewriteRule line to:
RewriteRule ^(.*)$ http://www.myanotherdomain.com/$1 [L,R,NC]

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.

Simple Apache 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]

Resources