Rewrite rule match ALL not working - mod-rewrite

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]

Related

CodeIgniter and specific rewrite rule

On my CodeIgniter site, I would like to add a specific rewrite rule, so that this url
http://www.exemple.com/cache.manifest
would rewrite to
http://www.exemple.com/controller/manifest
(because Safari 7 seems to only accept .manifest files for ApplicationCache)
So I try to add this line to my htaccess
RewriteRule ^cache.manifest$ controller/manifest
I added it before the other rewrite rules :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^cache.manifest$ controller/manifest
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
But it returns a 404. If I change the line to
RewriteRule ^cache.manifest$ test.html
it works. So the first part on my rule is correct.
If I try to access directly to www.example.com/controller/manifest, it works to, so my url is correct.
I tried also
RewriteRule ^cache.manifest$ index.php/controller/manifest [L]
But it doesn't work either…
Any clue ?
Thanks a lot
I tried some tests on my local server and I think the following might work:
RewriteRule ^cache.manifest$ /index.php/controller/manifest [R,L]
I am not entirely sure if you need the leading "/" or "/index.php", so you
may need to experiment.
You need the [R] flag to force a redirect. In this situation, you want Apache
to look for the string cache.manifest in the URL, and then go to the CI page
controller/manifest.
It appears that you need to explicitly set the redirect.
Please let me know if this works. Good luck!

mod_rewrite forward shortend URL

I am looking for a way to create a short URL path for a longer URL on my page
the long url is: domain.com/tagcloud/user.html?t=1234ABCD
i would like to offer a short version of the URL to easy access it:
domain.com/t/1234ABCD
I tried a few examples but I just don't get it how I could forward these rules.
RewriteRule ^(.*)/t/$ /tagcloud/user.html?t=$1 [L]
I am also using MODX so they already use rules.
in addition my htaccess file
RewriteEngine On
RewriteBase /
# Always use www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I must keep the code snippets above in my htaccess file. The first one simply forwards http://domain.com requests to www.domain.com
The friendly URLs part is needed to translate the internal IDs of my CMS with the alias of the URL. This feature must remain because the entire site cannot be influencted by the changes I try to make in htaccess...
I simply would like to add a listener that only if the URL matches www.domain.com/t/abcd1234
Therefore I need something that identifies the www.domain.com/t/ URL
your help is much appreciated
Try this:
RewriteCond %{REQUEST_URI} ^/t/.*
RewriteRule ^t/(.*)$ /tagcloud/user.html?t=$1 [R=301,L]

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 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 to do this via mod_rewrite

http://mydomain.com/bubba
goes to
http://mydomain.com/myscript.php?name=bubba
But does not match anything with an extension on it (.php, .html, etc).
I've been working on this for the last several hours and I cannot see how to do it. Every piece of documentation I find specifically doesn't work.
I'm doing this on a shared host (1and1.com) with .htaccess
Rewrites every non-existant file to myscript.php?name=requested:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ myscript.php?name=$1 [QSA,L]
Maybe you want RewriteCond -f, to check whether the file exists.
To only match things without a period:
^([^.]+)$ myscript.php?name=$1 [QSA,L]

Resources