RewriteRule translating url to use different php files - .htaccess

In the .htaccess file of my PHP application I use one simple rewrite to translate my URLs from:
/shop/hats/detroit/
to:
index.php?url=/shop/hats/detroit/
using the following rewrite rule:
# Rule 1
RewriteRule ^(.*)$ /repos/nvp/httpdocs/index.php?url=/$1 [L,QSA]
I would like the following rewrite to work as well:
# Rule 2
RewriteRule ^pic/(.*)$ /repos/nvp/httpdocs/get_image.php?pic=$1 [L,QSA]
But the two rules are kind of overwriting or conflicting with each other. They work each on their own but not together. How can I use all urls to use Rule 1 and only if url starts with pic/ to use Rule 2 instead of Rule 1?

This is because the pattern (.*) matches all uris .
In order to avoid the rules overriding ,you need to reorder your rules and put the specific rules first in order.
# specific rules
RewriteRule ^pic/(.*)$ /repos/nvp/httpdocs/get_image.php?pic=$1 [L,QSA]
#catch-all rules
RewriteRule ^(.*)$ /repos/nvp/httpdocs/index.php?url=/$1 [L,QSA]

Related

Apply htaccess rules for all cases except a few

I want every access to:
www.example.com/demo/action.html
to redirect to
www.example.com/demo/?section=action
Being action the variable that changes.
But, I do not want to apply that rule in a couple of cases, lets say:
www.example.com/demo/mypage1.html
www.example.com/demo/mypage2.html
For those cases I want them to load the real html pages mypage1.html and mypage2.html.
At the moment I have like 20 rules for all possible action variables. But as it doesn't seem to be ideal in terms of performance, I would rather have just the 2 special cases and a single rule for all the other cases.
This is what I have at the moment:
RewriteRule ^demo/removeUser.html$ demo/?section=removeUser [QSA]
RewriteRule ^demo/addUser.html$ demo/?section=addUser[QSA]
RewriteRule ^demo/addUser.html$ demo/?section=editUser[QSA]
RewriteRule ^demo/addUser.html$ demo/?section=comment[QSA]
... etc
You can keep exception at the top and then a generic rewrite rule:
RewriteEngine On
RewriteRule ^demo/(?:mypage1|mypage2)\.html$ - [L,NC]
RewriteRule ^demo/([\w-]+)\.html$ demo/?section=$1 [QSA,L,NC]
This assumes that demo/ directory has no .htaccess inside.
However if you are just looking at excluding all existing files from this rewrite then you just a single rule like this:
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^demo/([\w-]+)\.html$ demo/?section=$1 [QSA,L,NC]

.htaccess rewrite rule, change URL

I'm looking for the following to change the URL with two variables in a .htaccess file, from:
domain.com/folder/a123/url/index.php
domain.com/otherfolder/a321/url/index.php
to:
domain.com/folder/url/index.php
domain.com/otherfolder/url/index.php
I would prefer it in a single line .htaccess RewriteRule, if possible.
"folder" and "otherfolder" are the only two "folder names" and "a123"/"a321" can be anything starting with "a" and ending with random numbers.
You can do that in one with just one rule using this:
RewriteEngine on
RewriteRule ^(.+)/a[0-9]*/url/index.php$ /$1/url/index.php [R=301,NC]
But if you want to redirect all pages and not only index.php then use this:
RewriteRule ^(.+)/a[0-9]*/url/(.*)$ /$1/url/$2 [R=301,NC]
Also, if you want to rewrite the URLs instead of redirect them change [R=301,NC] to [NC].
Note that if you have that pattern in other URLs this rule will also apply, for example it will also redirect domain.com/adifferentfolder/a567/url/index.php to domain.com/adifferentfolder/url/index.php, if you have those type URLs and don't want to redirect them you need to use two rules instead:
RewriteRule ^folder/a[0-9]*/url/(.*)$ /folder/url/$1 [R=301,NC]
RewriteRule ^otherfolder/a[0-9]*/url/(.*)$ /otherfolder/url/$1 [R=301,NC]

How do i create only ONE rewrite line in htaccess

I have similar friendly url's like this:
http://localhost/galeria
http://localhost/galeria/
http://localhost/galeria/cat
http://localhost/galeria/cat/
I want to rewrite ONE rule in my htaccess file. So far, i was able to create 2 rules, like this:
RewriteRule ^galeria/?$ gallery/newest.php [L]
RewriteRule ^galeria/cat/?$ gallery/newest.php [L]
How do i create only ONE rewrite line for my 4 fiendly url's?
Thanks
You can use this single rule:
RewriteRule ^galeria(/cat)?/?$ gallery/newest.php [L,NC]
(/cat)? makes /cat optional thus matching all 4 URLs
RegEx Demo

.htaccess rewrite from one subdirectory to another

I have the following file:
/forums/faq.php -- at http://www.domain.com/forums/faq.php
I would like calls to this file (which are usually done in the form of faq.php?faq=faqname) to be rewritten as:
/faq/faqname -- at http://www.domain.com/faq/faqname
I've been trying the following .htaccess rules to no avail. On my root .htaccess:
RewriteRule ^forums/faq.php?faq=(.*)$ faq/$1 [L,R=301]
RewriteRule ^faq$ faq/ [L,QSA,R=301]
On my /forums/.htaccess:
RewriteRule ^faq.php?faq=(.*)$ http://www.domain.com/faq/$1 [L,R=301]
Could I be overlooking something really obvious here? Is my syntax off?
You can ditch the rules in the /forums/ htaccess, the rules there have precedence over rules in the document root. You don't need them in both places.
Your rules will cause a redirect loop if they did what you wanted.
You can't match against the query string in a RewriteRule, so the first rule won't match anything. What you really want to do is match against the actual request.
# redirect /forums/faq.php?faq=faqname to /faq/faqname
RewriteCond %{THE_REQUEST} \ /forums/faq\.php\?faq=([^&\ ]+)
RewriteRule ^forums/faq.php$ /faq/%1? [L,R=301]
Then you need to internally change it back
RewriteRule ^faq/(.+)$ /forums/faq.php?faq=$1 [L,QSA]

htaccess redirects from URLs on one domain to the root of another domain

Currently what is happening is people are accessing old URLs from google like icpaweb.com/site/pages/about-us/ and being sent to their corresponding urls on icpaweb.org : icpaweb.org/site/pages/about-us.
What I want is to send people from: icpaweb.com/site/pages/about-us to icpaweb.org/ without any of the succeeding url segments.
How do I do this?
If you have to use an .htaccess file, you can use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} icpaweb.com$ [NC]
RewriteRule .* http://icpaweb.org/ [R=301,L]
That will 301 redirect all requests for icpaweb.com to the index root of icpaweb.org. If you don't want 301, it can just be R.
You'll need to replace or turn off whatever mechanism is doing your redirecting now, they may not be compatible.
Use an url rewrite rule.
2 steps:
Write a RewriteCond so that the following rewrite rule only apply for url with host being icpaweb.com like RewriteCond %{HTTP_HOST} icpaweb.com$ [NC] The [NC] is for case insensitive match
Write a rewrite rule that convert all input to what you want like RewriteRule ^.*$ http://icpaweb.org/ [L]The [L] is to stop the rewriting to this rule if rule executed.

Resources