mod_rewrite- matching everything after domain - mod-rewrite

This rule works fine for http://foo.com/page/contact
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
But what I want to do is for http://foo.com/contact
But this is not correct:
RewriteRule ^/([^/\.]+)/?$ index.php?page=$1 [L]
How do I correct that?

Remove your first / that's not needed because the root directory is already there. like so:
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
your second example is technically asking for http://foo.com//contact

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]

URL Rewrite always taking the first condition

RewriteRule ^list lists.php [NC]
RewriteRule ^list/([^-]+)/ lists.php?listid=$1 [NC]
The problem is even if the link is like example.com/list/5 it is directed to list.php without the value. The second condition is not getting satisfied ever. How to solve this?
Try
RewriteRule ^list$ lists.php [L]
RewriteRule ^list/([\d]+)/$ lists.php?listid=$1 [L]
Notice that it will match list/5/ but not list/5. That would be:
RewriteRule ^list/([\d]+)$ lists.php?listid=$1 [L]
Edit: You can use [NC] if you want... I personally like the [L] flag and the case wording respected.

Mod_rewrite Rules for MVC Architecture

In my site you can open pages like this...
example.com/?controller=index&action=index
I want to make links nicer so I use mod_rewrite...
RewriteRule ^([^/]+)/(.*)$ index.php?controller=$1&action=$2 [L,QSA]
It works perfectly and result is somehow like this...
example.com/index/index
But my problem is that this link, where is set only the 1st variable won't work. Don't know how to fix it! This won't work...
example.com/index
Do something like this:
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/(.*)$ index.php?mod=$1&act=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9_-]+)/(.*)$ index.php?mod=$1 [QSA,L]
RewriteRule ^(.*)$ index.php [QSA,L]

mod_rewrite: How to match file in directory or in root depending on 'availability'?

Hello, long time listener, first time caller here!
Thank you for the excellent advice you all share.
I have these mod_rewrite rules set up:
RewriteRule ^([^/.]+)/([^/.]+)/?$ $1--$2.php?%{QUERY_STRING} [L]
RewriteRule ^([^/.]+)/?$ $1.php?%{QUERY_STRING} [L]
They make /company/services/ redirect to company--services.php. Or /company/ to company.php. Works perfect.
But now I'd like to another rule that if I were to put services.php inside physical directory /company/ it will match and redirect that. And if failing that, look for my initial rule. (And failing that, return 404.)
I figured it would be as simple as including:
RewriteRule ^([^/.]+)/([^/.]+)/?$ $1/$2.php?%{QUERY_STRING} [L]
but not so. It will returns a 404 instead. I'm a bit stumped as this goes against how I believed mod_rewrite to work (if a rule does not match, go to the next one.)
Thank you for any pointers!
Found it. For reference, this is the complete set of rules:
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
RewriteRule ^([^/.]+)/([^/.]+)/?$ /$1/$2.php?%{QUERY_STRING} [L]
RewriteRule ^([^/.]+)/([^/.]+)/?$ $1--$2.php?%{QUERY_STRING} [L]
RewriteRule ^([^/.]+)/?$ $1.php?%{QUERY_STRING} [L]

mod_rewrite to transform old request urll into new one

.hello - i need to transform old url requests to fit into the new sites content;
ie 'art-consultancy' used to be 'consultancy' so how can i grab 'consultancy' urls and transform them into 'art-consultancy'
MY RULE if ^consultancy$ MAKE ^art-consultancy$ and continue to the rules below...
RewriteRule ^art-consultancy$ consultancy-02.php [L]
RewriteRule ^art-consultancy/$ consultancy-02.php [L]
RewriteRule ^art-consultancy/([a-zA-Z0-9\-]+)$ consultancy-02.php?section=$1 [L]
RewriteRule ^art-consultancy/([a-zA-Z0-9\-]+)/$ consultancy-02.php?section=$1 [L]
#
RewriteRule ^art-consultancy/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)$ article-01.php [L]
RewriteRule ^art-consultancy/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/$ article-01.php [L]
any help appreciated!
ps. tried Redirect /consultancy /art-consultancy without any luck. Think this method needs an actual file?
best, Dc
You basically just have to do exactly what you said you wanted to do, in a similar way that you've done with the other rules, so I'm not sure how much this qualifies in the way of an "answer"...But, for the sake of completeness, I'll go ahead and write up the full thing:
(Also, I condensed your other rules into single lines)
# Add in this condition because consultancy-02.php matches here too
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^consultancy(.*)$ art-consultancy$1
RewriteRule ^art-consultancy/?$ consultancy-02.php [L]
RewriteRule ^art-consultancy/([a-zA-Z0-9\-]+)/?$ consultancy-02.php?section=$1 [L]
RewriteRule ^art-consultancy/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ article-01.php [L]
If you wanted consultancy to be transformed to art-consultancy in the user's browser URL, you should replace the first RewriteRule with this:
RewriteRule ^consultancy(.*)$ /art-consultancy$1 [R=301,L]
I'm not entirely sure why the Redirect didn't work like you expected it to though. If you look at your server's error_log it might tell you, but otherwise it's hard to speculate without knowing what your site's directory structure looks like.

Resources