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

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]

RewriteRule doesn't do anything

I am using the current set of re-write rules in my .htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule lessons/(.*)$ page.php?url=$1
It works fine. Now I have some old pages which I would like to redirect to new pages. I do the following:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule lessons/(.*)$ page.php?url=$1
RewriteRule old/this_url/(.*)$ lessons/to_this_url/and_this_url
But when I go to www.mywebsite.com/old/this_url/another_path it simply doesn't do anything?
I tested the rule in a re-write test program and it worked. The rule was recognised. So not sure, what I am doing wrong on the live website?
Answering my own question. In fact I realized a bit late that what I was trying to do was redirecting the content of an old website to a new website. They live under the same domain name but don't live under the same root. One lives under / (the current one) and the old one lives under /old/. So the rules needed to be added to the .htaccess of the old website. In:
www.root.com/old/.htaccess (THIS IS CORRECT)
While I was trying to edit them in:
www.root.com/.htaccess (NO)
That solves my problem.

Rewrite rules confused

I am struggling to achieve this simple thing...
I have some static pages which should be like
www.domain.com/profile etc..
The problem is how to write the rewrite rules in order to ..
There would be some fixed rewrites
like /home
I want every file that exists not to be rewritten
www.domain.com/test.php should go to
test.php
Lastly if it is not found i want it to be redirected to static.php?_.....
RewriteRule ^/home/?$ /index.php?__i18n_language=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]+)/?$ /static.php?__i18n_language=$1
This works ok but if i type index.php or test.php or even the mach from other redirection it gets me in static.php...
Please help!
According to your description you can use these rules:
# stop rewriting process if request can be mapped onto existing file
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteRule ^ - [L]
# rewrite known paths /home, /foo, /bar, etc.
RewriteRule ^/(home|foo|bar|…)$ /index.php [L]
# rewrite any other path
RewriteRule ^ /static.php [L]
I haven't used this in a long time, but it's something I found, that should help. It is part of an old script that generates .httaccess files for redirecting from /usr/share/doc only when the doc isn't found:
The rule is "Check, and if the target url exists, then leave":
RewriteEngine On
RewriteBase /doc/User_Documents
### If the directory already exists, then leave
### We're just redirecting request when the directory exists but has been renamed.
RewriteCond %{REQUEST_FILENAME} User_Documents/([^/]+-[0-9][^/]*)
RewriteCond $PWD/%1 -d
RewriteRule .* - [L]
It's the [L] that means leave if one of the conditions is matched. In the old script, there are hundreds of generated rules (after [L]) that are skipped, because one of the conditions matched. In your case you would skip the rest of the rules when the target %{REQUEST_FILENAME} is found.
So, I suggest, before the redirection rule:
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]

Rewrite URLs for static content

I have problems with static files url rewriting in current .htaccess setup on apache2.
My app structure is:
/siteroot
/siteroot/app
/siteroot/lib
/siteroot/...
/siteroot/public <- all the static files (images, js, etc.) stored here
/siteroot/index.php
/siteroot/.htaccess
So, i need to rewrite url like /css/style.css to /public/css/style.css. I did that in really simple way, but when the file is not found it causing 10 internal redirects, which is bad. I need somehow to return 404 code if file not found, or just pass it to the next rule. And i dont have any access to site configuration file. Only .htaccess.
The reason why i`m asking this question is that the site was running on nginx and i need to rebuild the same configuration on apache.
Here is my .htaccess file.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.+\.(jpg|jpeg|gif|png|ico|css|js|swf)$ /public/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Test if a redirect is reasonable:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/public/$0 -f
RewriteRule ^.+\.(jpg|jpeg|gif|png|ico|css|js|swf)$ /public/$0 [L]
If the number of prefixes is limited, you could add another group to your regex:
RewriteRule ^(css|js|images|etc)/.+\.(jpg|jpeg|gif|png|ico|css|js|swf)$ /public/$0 [L]

xampp redirects

Does anyone has an example how to do a rule in .httpaccess in xampp?
I'm trying to redirect from localhost/test/company.php?name=Abc to localhost/test/company/Abc and I cant seem to find the solution. I followed some examples that I found on the web but none seems to work. I'm putting the .htacces file in the same folder where I have the company.php file. And I have the urlrewrite turned on.
Put the .htaccess file in your application root.
The .htaccess file should have something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^company/([a-zA-Z0-9\-]+)$ /company.php?name=$1 [L,NC]
Turn on mod_rewrite and restart apache.
Then it should work.

Resources