Apache RewriteCond seems to be ignored? - apache

I have the following code:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/wp-admin
RewriteRule ^(.+)$ http://example.com/$1 [R=permanent,L]
I have tried various variations of above, but currently the wp-admin rewrite condition is ignored.
I don't want "/wp-admin" directory and "/" root requests redirected! (Yes, I know, sounds funky)
What am I doing wrong in above mod_rewrite?

My bad.
For anyone in similar situation, you also need
RewriteCond %{REQUEST_URI} !^/wp-login
(since wp-admin redirects to wp-login)

Related

rewrite url to subdirectory

I'm new at url rewriting using appache.
I have a htaccess file with those rules:
RewriteCond %{HTTP_HOST} ^hostname/* [NC]
RewriteRule http://hostname/subDir/* [L,R]
The purpose of the rules is for rewriting the request from http://hostname to http://hostname/subDir.
I tested the rules using http://htaccess.mwl.be/ but cannot make the RewriteRule work. Tried different rules but non of them works.
Can someone help me with this simple rule?
It is a bit unclear what exactly do you want to achieve, but something like this should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} =www.example.com [NC]
RewriteCond %{REQUEST_URI} !^/subDir [NC]
RewriteRule ^ subDir%{REQUEST_URI} [L]

Cleaning urls my htaccess

I have looked for answers but none have worked for me. I want to clean up my urls removing the page name, extension and variables on all my pages in all folders.
so
http://www.example.co.uk/home/index.php?username=ben&id=1
to
http://www.example.co.uk/home/
and
http://www.example.co.uk/profile/index.php?blog=test&id=45
to
http://www.example.co.uk/profile/
ect....
im running apache
thanks
sorry for late reply. so far i have
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+\.)?example\.co\.uk$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.example.co.uk%{REQUEST_URI} [R=301,L]
To force the www. and also remove the file name and extension
You need to enable mod rewrite module in your apache configuration.
In your .htaccess file try some basic rewrite rule.
If you rewrite
http://www.example.co.uk/home/index.php?username=ben&id=1
to
http://www.example.co.uk/home/
you are losing the parameters "username=ben&id=1".
Some rewrite tutorials from google sesarch
https://www.google.com/search?q=mod+rewrite+tutorial&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&channel=fflb
http://www.sitepoint.com/guide-url-rewriting/
http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/

mod_rewrite for clean URL's not working

I have looked at every question and every example of reWriteRule for an apache server... Nothing has worked.
I just need to change
http://beta.EXAMPLE.com/profile.php?profile_name=ntgCleaner
to
http://beta.EXAMPLE.com/ntgCleaner
I have to see if my .htaccess file is being read and checked to see if mod_rewrite is enabled and they both work perfectly fine.
For some reason, the examples that I have come across are not working for me. Here are some of the examples.
RewriteEngine On
RewriteRule /(.*)$ /profile.php?username=$1
,
RewriteEngine On
RewriteBase /
RewriteRule ^profile\/(.*)$ ./profile.php?profileId=$1 [L,NC,QSA]
and
Options +FollowSymlinks
RewriteEngine on
RewriteOptions MaxRedirects=10
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?profile_name=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?profile_name=$1
But none of these work. Is this because I am using a subdomain? I plan on eventually switching over the subdomain from BETA to just www when I finish the site.
Any advice?
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ /profile.php?profile_name=$1 [L,QSA]
Your examples have a lot of different query strings in it, so I'm not sure which one is the one you really want. You have ?username=, ?profileId=, ?profile_name=.
You must use condition (RewriteCond) and when is condition met, rule (RewriteRule) is applicated. So, when you don't use condition, rule is not used, because engine don't know for what to use it.
RewriteCond must be used before RewriteRule, I use it against web-bots in this form:
RewriteCond %{REMOTE_HOST} .googlebot.com$ [NC,OR]
RewriteCond %{REMOTE_HOST} .search.msn.com$ [NC,OR]
RewriteCond %{REMOTE_HOST} .kimsufi.com$ [NC]
RewriteRule ^.*$ /errors/404.html [F]

Why would this .htaccess be stripping out containing folder?

This is from a .htaccess located under /~new/
# invoke rewrite engine
RewriteEngine On
# force domain.com to www.domain.com
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$0 [R=301,L,NC]
When accessing http://domain.com/~new/hello, it is being rewritten to http://www.domain.com/hello
The www. is being added in like it should, but for some reason it is ignoring the /~new/ subdirectory.
Does anyone know what may be causing this? FYI, there is a .htaccess in the TLD but it is empty. I know I could tack on /~new/ to the regex replacement string, but I'd prefer a generic solution (for portability) and I am not sure why it's stripping it out in the first place. I have also tried playing around with RewriteBase but could not get it to work.
Thanks
I think normally the {REQUEST_URI} would be in your rewrite rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www..*
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]*).(com|com/)
RewriteRule ^.*$ www.%1.%2%{REQUEST_URI} [R=301,L]

Apache rewrite based on subdomain

I'm trying to redirect requests for a wildcard domain to a sub-directory.
ie. something.blah.example.com --> blah.example.com/something
I don't know how to get the subdomain name to use in the rewrite rule.
Final Solution:
RewriteCond %{HTTP_HOST} !^blah\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)
RewriteRule ^(.*) /%1/$1 [L]
Or as pointed out by pilif
RewriteCond %{HTTP_HOST} ^([^.]+)\.blah\.example\.com$
You should have a look at the URL Rewriting Guide from the apache documentation.
The following is untested, but it should to the trick:
RewriteCond %{HTTP_HOST} ^([^.]+)\.blah\.domain\.com$
RewriteRule ^/(.*)$ http://blah.domain.com/%1/$1 [L,R]
This only works if the subdomain contains no dots. Otherwise, you'd have to alter the Regexp in RewriteCond to match any character which should still work due to the anchoring, but this certainly feels safer.
Try this:
RewriteCond %{HTTP_HOST} (.+)\.blah\.domain\.com
RewriteRule ^(.+)$ /%1/$1 [L]
#pilif (see comment): Okay, that's true. I just copied a .htaccess that I use on one of my projects. Guess it has a slightly different approach :)
#Sam
your RewriteCond line is wrong. The expansion of the variable is triggered with %, not $.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.media\.xnet\.tk$
^
that should do the trick