mod rewrite condition fix to avoid partial word match - regex

Its probably very simple, but I can't make it work =/
I have this rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin/?(.+?)?$ adm.php?route=$1&%{QUERY_STRING} [L]
Well, its suppose to send the user to the adm.php when there is a localhost/admin or localhost/admin/anything url, preserving the query string.
It works as expected, except for the fact it's matching any word that starts with 'admin', for example:
localhost/admin/news/list?page=1 => adm.php?route=news/list&page=1
localhost/adminbool/news/list?page=1 => adm.php?route=bool/news/list&page=1
Both of rewrite works, but that extra 'bool' messes everything in my routing process.
How can I make it respond only to a 'admin' exact match?
Any ideas?
Thanks!
EDIT
I guess im not clear enought.
Here is the full .htaccess and a brief comment about how its suppose to work
Options -Indexes
Options -MultiViews
<FilesMatch "\.(tpl|ini|log|txt)">
Order deny,allow
Deny from all
</FilesMatch>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(adm/|app/|lib/|sys/) - [F,L,NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!admin/?$)(.+?)/?$ index.php?route=$1&%{QUERY_STRING} [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin(/.*)?$ adm.php?route=$1 [L,QSA]
</IfModule>
the index.php file refers to /app (front-end)
the adm.php refers to the /adm (back-end)
the urls of the front-end has no prefix, while the back-end urls has the /admin/ prefix
example:
localhost/admin - back-end
localhost/admin/ - back-end
localhost/admin/news/list?order=date - back-end
localhost/ - front-end
localhost/admine - front-end
localhost/administration-whatever.html - front-end
localhost/admine123-anything.html - front-end

Couple of issues:
Your regex is incorrect
You're not using helpful QSA flag.
Use this rule with correct regex and QSA:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin(/.*)?$ adm.php?route=$1 [L,QSA]
QSA (Query String Append) flag preserves existing query parameters while adding a new one.
Your rewrite rules:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(adm/|app/|lib/|sys/) - [F,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin(/.*)?$ adm.php?route=$1 [L,QSA]
RewriteCond %{REQUEST_URI} !/adm\.php [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!admin/?$)(.+?)/?$ index.php?route=$1 [L,QSA,NC]

Try this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin[$|/*](.+?)?$ adm.php?route=$1&%{QUERY_STRING} [L]

Related

Remove the query string from url using htaccess

I need some help with htaccess. Would you be so kind to assist a little bit?
I have a URL like this
https://example.com/index.php?p=application-intelligence
[or]
https://example.com/?p=application-intelligence
Basically the index.php is passed some parameter 'home' to know which page to load i.e. home.php
So I've tried to follow your post on your blog but with not much luck.
So I'd like the final code to be
https://example.com/application-intelligence
Here's my code.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?p=$1
Also for the
https://example.com?p=home
I'd like it to be just
https://example.com
You may use these rules:
RewriteEngine on
# handle /?p=home
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?p=home\s [NC]
RewriteRule ^ /? [R=301,L,NE]
# handle /?p=<something>
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]
This is what i have in my apache virtual host conf file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

rewrite rule check for string existence

i need a RewriteRule which changes an url of http://domain.org/foo/bar to http://domain.org/de/foo/bar but does nothing if there is an url like http://domain.org/en/foo/bar or http://domain.org/de/foo/bar
so if there is no en/ and no de/ then it should add de/, else it should do nothing
how can this be done? i already played around a bit with regex, but I dont know how to check if there is en/ or if there is no en/.
help is highly appreciated.
UPDATE:
my current rules are the default rules you should have when running wordpress:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
UPDATE2:
this is wordpress and de/ and en/ are not actual folders.. they are used by a multilanguage plugin. since i have some issues with this plugin i need the redirect as described above.
In general without any other rules, this would redirect anything that doesn't start with /en/foo/bar.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/en [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /de/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
You can use this rule in your root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!(?:en|de)).*)$ de/$1 [L,NC,R]
Keep this rule before default WP rule.
EDIT Full .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!(?:en|de)).*)$ de/$1 [L,NC,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Htaccess rewrite Single, or multiple $_GET parameter on LINUX base server

Below's url is I need .htaccess rewrite from ?view=home to /view/home
http://example.com/?view=home
And what if my url is quite dynamic with $_GET Parameter that have multiple $_GET?
http://example.com/?view=home&url=this&name=test&id=that
I had try few rewrite module but cant get result that I wanted.
Below is my rewrite engine :
Options +FollowSymlinks
RewriteEngine On
# specific rule
RewriteCond %{QUERY_STRING} ^([^=]+)=([^&]+)
RewriteRule ^(constant)$ /index.php?view$1 [L]
# general catch-all
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]
My current web hosting is on Linux Web Hosting, locally running my tested file is on XAMPP environment.
You can this code to redirect from ?view=home to /view/home:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{THE_REQUEST} /\?([^=]+)=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?$1=$2 [L,QSA]
# specific rule
RewriteCond %{QUERY_STRING} ^([^=]+)=([^&]+)
RewriteRule ^(constant)$ /index.php?view$1 [L]
# general catch-all
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]

.htaccess - url rewrite that drops extension and passes argument

I'm looking for series of .htaccess statements that will convert the following urls
http://mysite.com/product to http://mysite.com/product.php
http://mysite.com/product/55 to http://mysite.com/product.php?id=55
http://mysite.com/category/38 to http://mysite.com/category.php?id=38
http://mysite.com/resources/car/19 to http://mysite.com/resources/car.php?id=19
http://mysite.com/resources/car/19?color=red&year=2013 to http://mysite.com/resources/car.php?id=19&color=red&year=2013
In other words, when rendering php files in my website, i want to drop the .php extension. If a url ends with a number, then i want to pass that as the id query string parameter. I also want to pass all the conventional query string parameters to my php my file like color and year.
I'm not sure how to construct such a .htaccess file.
ADDITIONAL NOTES
I'm currently using hte following, but it fails to take into consideration urls that trail with a number, and passing that along as id
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{QUERY_STRING} (.*)
RewriteRule . %{REQUEST_FILENAME}.php?%1 [L]
If I can do something like replace the trailing number in REQUEST_FILENAME in line two, thatwould be great.
First you need to make sure Multiviews is turned off. Then you'll need 3 sets of rewrite rules:
Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([0-9]+)$ /$1.php?id=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^resources/([^/]+)/([0-9]+)$ /resources/$1.php?id=$2 [L,QSA]
You can be a little more specific if the URLs are actually just "product", "category" and "car", then you can just have:
Options -Multiviews
RewriteEngine On
RewriteRule ^product$ /product.php [L]
RewriteRule ^(product|category)/([0-9]+)$ /$1.php?id=$2 [L,QSA]
RewriteRule ^resources/car/([0-9]+)$ /resources/car.php?id=$1 [L,QSA]
John (the op) says:
This was the final .htaccess file i ended up with
RewriteEngine On
Options -Multiviews
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)\/([0-9]+)$ $1.php?id=$2&%1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)$ $1.php?%1 [L]

rewrite rule for WordPress 3.3 permalinks is not working

Every since an upgrade to WordPress 3.3 URLs are not redirecting as they should.
Changed: domain.com/2010/10/postname/ to: domain.com/postname/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/[0-9]{4}/[0-9]{2}/(.+)$ /$1 [NC,R=301,L]
The problem was due to the leading slash and not using $3
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]{4})/([0-9]{1,2})/(.+)$ /$3 [NC,R=301,L]
There's a script here you can use to generate .htaccess rules if you want to change permalinks to the /%postname%/ structure.
http://yoast.com/change-wordpress-permalink-structure/
My permalinks were exactly the same as yours, I used this tool to change them and it is working well.
The last rule will never get applied if the previous rule matches. Assuming that the http://domain.com/2010/10/postname/ request doesn't match a file or directory, the RewriteRule . /index.php [L] is going to rewrite the URI to /index.php thus it'll never get to your rule. Try moving your rule up to the top, just below RewriteBase /, and duplicate the !-f/!-d conditions, so that it looks like this:
RewriteBase /
# for 301 redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/[0-9]{4}/[0-9]{2}/(.+)$ /$1 [NC,R=301,L]
# the rest of the rules
RewriteRule ^atom.xml$ feed/ [NC,R=301,L]
RewriteRule ^rss.xml$ feed/ [NC,R=301,L]
RewriteRule ^rss2.xml$ feed/ [NC,R=301,L]
RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC]
RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/handle [R=302,NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Also, if this is in an .htaccess file, you need to remove the leading slash in the rule match so that it looks like this: ^[0-9]{4}/[0-9]{2}/(.+)$