RewriteRule all except other rewrite already declared - apache

I have the following entries handled as fixed files using .htaccess (for example)
RewriteEngine On
RewriteRule ^redir$ redir_base64.php
RewriteRule ^confirm$ confirm.php
RewriteRule ^migrate$ migrate.php
RewriteRule ^import_conn$ import_conn.php
RewriteRule ^callback_ot$ callback_ot.php
RewriteRule ^callback_yh$ callback_yh.php
RewriteRule ^callback_gp$ callback_gp.php
RewriteRule ^callback_lk$ callback_lk.php
RewriteRule ^results/map$ index.php
RewriteRule ^results$ index.php
RewriteRule ^robots\.txt$ robots.php
RewriteRule ^sitemap\.xml$ sitemap.php
RewriteRule ^removeml$ removeml.php
RewriteRule ^rss$ rss_gen.php
RewriteRule ^cpt\.jpg$ c/captcha/captcha.php
RewriteRule ^cptfrm\.jpg$ c/captcha/captcha.php?width=120&height=60&characters=4
RewriteRule ^cptfrm160\.jpg$ c/captcha/captcha.php?width=100&height=60&characters=3
RewriteRule ^login/lk$ login_conn.php?op=lk
RewriteRule ^t$ c/thumb/phpThumb.php
RewriteRule ^spce$ spce.php
RewriteRule ^orplkd$ order_publisher.php
RewriteRule ^ord_ren$ order_reactivate.php
RewriteRule ^logout$ logout.php
This entry works fine! But I need to access /000/000 and I see that an conflict with lines indicates below.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\d\w\-\.]+)/([\d]+)$ index.php [QSA,L]
RewriteRule ^([\d\w\-\.]+)$ index.php [QSA,L]
I try to access to my site using that lines but I cannot to recognize only the last lines because use ALL access on the site.
I hope so understand my help.
Thanks!

You need to repeat the 2 conditions. Rewrite conditions only apply to the immediately following condition.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\d\w\-\.]+)/([\d]+)$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\d\w\-\.]+)$ index.php [QSA,L]

Related

How to fix .htaccess RewriteRule conflict?

I have conflict RewriteRule .htaccess.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
RewriteRule ^unsubscribe.php$/template/site.org/newsletter/unsubscribe.php
RewriteRule ^(unsubscribe)/?$ /$1.php [L,NC]
This
RewriteRule . /index.php [L]
Conflict with
RewriteRule ^unsubscribe.php$/template/site.org/newsletter/unsubscribe.php
RewriteRule ^(unsubscribe)/?$ /$1.php [L,NC]
Please help me to fix it.
Your unsubscribe rule needs a space between pattern and target but more importantly you need to place these rules before catch-all index.php rule. You can also combine 2 last rules into one:
RewriteEngine on
RewriteBase /
RewriteRule ^unsubscribe(?:\.php)?$ /template/site.org/newsletter/unsubscribe.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]

Rewrite htaccess so it doesn't need to repeat itself

I have one big htaccess file that does all the redirects for a site. Here is what I do for each directory:
RewriteRule ^CAKE_MIX/.php$ CAKE_MIX/index.php [R,L]
RewriteRule ^CAKE_MIX$ CAKE_MIX/ [R,L]
RewriteRule CAKE_MIX/faqs.php$ /faqs/faqs.php?cat=CAKE_MIX [QSA]
RewriteRule CAKE_MIX/index.php$ /overview/overview.php?cat=CAKE_MIX [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /CAKE_MIX/*
RewriteRule . /faqs/faqs.php?cat=CAKE_MIX&question=%{REQUEST_URI}
RewriteRule ^COOKIE_DOUGH/.php$ COOKIE_DOUGH/index.php [R,L]
RewriteRule ^COOKIE_DOUGH$ COOKIE_DOUGH/ [R,L]
RewriteRule COOKIE_DOUGH/faqs.php$ /faqs/faqs.php?cat=COOKIE_DOUGH [QSA]
RewriteRule COOKIE_DOUGH/index.php$ /overview/overview.php?cat=COOKIE_DOUGH [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /COOKIE_DOUGH/*
RewriteRule . /faqs/faqs.php?cat=COOKIE_DOUGH&question=%{REQUEST_URI}
And I do this for 18 different directories. How would I rewrite the htaccess to say something like "for any of the following directories: COOKIE_DOUGH, CAKE_MIX,ICE_CREAM, follow these rules?" and then the rules could be like:
RewriteRule ^DIRECTORY/.php$ DIRECTORY/index.php [R,L]
And would only have to be written once? Is that possible?
You can use regex alternation:
RewriteRule ^(CAKE_MIX|COOKIE_DOUGH|ICE_CREAM)/\.php$ $1/index.php [R,L]
RewriteRule ^(CAKE_MIX|COOKIE_DOUGH|ICE_CREAM)$ $1/ [R,L]
RewriteRule (CAKE_MIX|COOKIE_DOUGH|ICE_CREAM)/faqs\.php$ /faqs/faqs.php?cat=$1 [QSA]
RewriteRule (CAKE_MIX|COOKIE_DOUGH|ICE_CREAM)/index\.php$ /overview/overview.php?cat=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(CAKE_MIX|COOKIE_DOUGH|ICE_CREAM)/
RewriteRule . /faqs/faqs.php?cat=%1&question=%{REQUEST_URI}

htaccess issue in accessing the directory

My Blog link is http://www.example.com/blog when i am trying to access it it converts the url into http://www.example.com/blog/?ID=blog which is not correct. But when i put the backslash at the end of the blog it works fine but i don't want this back slash at the end
I try to change every thing inside htaccess file but failed.
Here is my htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ - [L]
RewriteRule ^products products.php
RewriteRule ^websitemap sitemap.php
RewriteRule ^aboutus aboutus.php
RewriteRule ^clients clients.php
RewriteRule ^shoppingcart inq.php
RewriteRule ^contactus contact.php
RewriteRule ^finished finished.php
RewriteRule ^product/([a-zA-Z0-9_-]+)$ product_large.php?ID=$1
RewriteRule ^([a-zA-Z0-9_-]+)$ category.php?ID=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ products.php?ID=$2&MID=$1
ErrorDocument 404 http://www.example.co.uk/error404.php
RewriteCond %{HTTP_HOST} ^example.co.uk$
RewriteRule ^/?$ "http\:\/\/www\.example\.co\.uk\/" [R=301,L]
Insert this rule after RewriteRule ^ - [L] to force a trailing slash:
DirectorySlash On
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^blog(/.*)?$ - [L,NC]
# your rest of the rules

.htaccess redirect trouble

I'm having trouble with (what should be) a simple .htaccess redirect. I need to changed any url that has "for_homes" into "for_home".
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
However when I go to any pages with for_homes I get a 404.
Rewrite Rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
</IfModule>
The problem is a preceeding rule is rewriting to index.php. In this block here:
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
### This rule rewrites "for_homes" to "index.php?/for_homes"
RewriteRule ^(.*)$ index.php?/$1 [L]
### These rules never get applied
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
So perhaps after the RewriteCond %{REQUEST_FILENAME} !-d, add conditions to exclude the later 2 rewrites, So that it looks like this:
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/for_home(s)?
RewriteCond %{REQUEST_URI} !^/for_business(es)?
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
If it's your intention to have index.php handle for_home and for_businesses, so that when someone enters http://domain.com/for_homes/stuff in their browser's address bar, the browser gets redirected to http://domain.com/for_home/stuff, then it gets internally rewritten to /index.php?/for_home/stuff so that index.php can handle the request, you simply need to move the 301 redirects before the index.php rewrite:
### Redirect first
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Perhaps you are just missing RewriteEngine On
RewriteEngine On
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]

Add directory in the middle of a URL with hataccess

My current .htacces file looks like this:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
What I am trying to accomplish (as well as the above) is to change a url such as http://domain.com/pages/pagename to something like http://domain.com/index.php/pages/view/pagename.
Keeping in mind that I still require urls without the /page/ part such as http://domain.com/search to go to http://domain.com/index.php/search. I am working with CodeIgniter.
What I have come up with so far is:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
RewriteCond %{REQUEST_URI} ^/pages/(.*)
RewriteRule ^(.*)$ ./index.php/pages/index/$1 [L,QSA]
But it isn't working.
Try this one:
RewriteEngine on
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# /pages/ specific rule
RewriteRule ^pages/(.*)$ index.php/pages/index/$1 [L]
# everything else
RewriteRule ^(.*)$ index.php/$1 [L]
The above assumes that http://domain.com/pages/ is not a real folder.
RewriteRule ^pages/(.*) /index.php/pages/index/$1