mod_rewrite for clean URL's not working - apache

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]

Related

How to add/ignore a subfolder in htaccess file?

I have build an app at http://url/sdf19/
I have a .htaccess placed in /sdf19/ containing RewriteRule for clean urls.
But I have built a PDF generating tool, which is in a subfolder /inc/tools.
I need to link to it direct to run before headers.
Despite a few hours of searching, trying snippets, generators, etc. I cannot get any request to http://url/sdf19/inc/tools to be allowed, without the existing RewriteRule set taking over
Here is my starting file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)\/?$ index.php?page=$1 [NC]
RewriteRule ^([a-z]+)\/([0-9]+)\/?$ index.php?page=$1&id=$2 [NC]
RewriteRule ^([a-z]+)\/([a-z]+)\/?$ index.php?page=$1&action=$2 [NC]
RewriteRule ^([a-z]+)\/([a-z]+)\/([0-9]+)\/?$ index.php?page=$1&action=$2&id=$3 [NC]
I've tried to add this on line 4;
RewriteCond %{REQUEST_URI} !^ inc/tools/ [NC]
This gave RewriteCond: bad argument line error
I've tried to add this to line 2;
RewriteCond %{REQUEST_URI} ^/inc/tools/(.*)$
RewriteRule ^.*$ - [L]
Desired result is that I can allow direct access to http://url/sdf19/inc/tools > everything I have tried so far i get redirected to base http://url/sdf19/
Maybe, we could step by step solve this problem. First step, we might just want to make something work with as less as boundaries that'd be possible.
Let's design a low boundary expression, maybe something similar to:
(.+)(\/inc\/tools)
From here, we can just add \/inc\/tools to the RewriteCond, just for testing. Later, we can modify that.
RegEx
If this wasn't a desired expression, you can modify/change your expressions in regex101.com.
RegEx Circuit
You can also visualize your expressions in jex.im:
RewriteRule Test
You can test your RewriteRules in htaccess.madewithlove.be.
I'm not so sure about the rest of RewriteRules, but I'm assuming they are working fine and not conflicting with the new one. Maybe, this or something similar would work:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} \/inc\/tools [NC]
RewriteRule ^(.*)$ $1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)\/?$ index.php?page=$1 [NC]
RewriteRule ^([a-z]+)\/([0-9]+)\/?$ index.php?page=$1&id=$2 [NC]
RewriteRule ^([a-z]+)\/([a-z]+)\/?$ index.php?page=$1&action=$2 [NC]
RewriteRule ^([a-z]+)\/([a-z]+)\/([0-9]+)\/?$ index.php?page=$1&action=$2&id=$3 [NC]
</IfModule>

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 not working correctly

I have written a simple mod rewrite script to turn website.com/index.php?var1=1&var2=2&var3=3 into website.com/index/1/2/3. For some reason it's not working, any ideas why?
#RewriteRule ^$ index.php [QSA]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1\.php [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [QSA]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ comments.php?var1=$1&var2=$2&var3=$3
The first rule rewrites index/1/2/3 to index/1/2/3.php.
then the second rule rewrites this to index.php/index/1/2/3.php
and finally the last rule doesn't match.
If you want to rewrite website.com/index.php?var1=1&var2=2&var3=3 into website.com/index/1/2/3, you must capture the query string arguments with RewriteCond and insert them into a RewriteRule
RewriteEngine on
RewriteCond %{QUERY_STRING} var1=(.*?)&var2=(.*?)&var3=(.*?)
RewriteRule ^index.php$ /index/%1/%2/%3 [L]
If you want to redirect instead of internally rewrite, i.e. show the new URL in the browser, use [R,L] as RewriteRule flags
RewriteRule ^index.php$ /index/%1/%2/%3 [R,L]
It looks like the rewrite on line 6 will catch that and send it to index.php. It's hard to tell not knowing what things are files and directories, but I would guess that it isn't. If you have access to the httpd.conf you can turn up the rewrite logging, set
RewriteLogLevel 8
And it will log far more information than you need to debug this. On newer versions of apache the setting is set with LogLevel e.g..
LogLevel alert rewrite:trace8
which may be allowed in vhosts or htaccess so look it up in your version's docs.
Don't leave this on as it will seriously impact performance.

Check URL and stop processing using htaccess

I want to check URL using htaccess. Developer might want run special file - specialfile.php. I use htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /specialfile\.php$
RewriteRule .* [L] #don't change adress
RewriteRule ^$ public/index.html [NC,L]
RewriteRule (.*) public/$1 [NC,L]
My idea was: if rewriteCond %{REQUEST_URI} !/specialfile.php$ true than htaccess should use RewriteRule .* [L] - that should mean that specialfile.php will be run and this all. But it doesn't work because it runs next rule: RewriteRule (.*) public/$1 [NC,L].
I think you are using the RewriteCond not correctly. The conditions only affect the next RewriteRule that follows.
Check out the example on the Apache Homepage. Since your 2nd RewriteRule is evalutated, I think your conditions are not correct. To get a litte bit more information about the rewriting, you should increase the log level. This is also documented here.
Your 2nd rule ^$ matches only an empty request btw. That's why it probably does not work as you expect it to.

Apache RewriteCond seems to be ignored?

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)