rewrite rule for Everything but certain pattern - regex

I want http://my_domain.com/forum/index.php/blah_blah_blah to not being redirected to anywhere.
But I want http://my_domain.com/something_else_that_is_not_forum/blah_blah_blah to be redirected to http://my_domain.com/index.php/something_else_that_is_not_forum/blah_blah_blah
So basically, only everything without http://my_domain.com/forum/ prefix should be redirected.
I have this .htaccess:
<IfModule mod_rewrite.c>
# Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteBase /
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^(?!forum.*)(.*)$ index.php/$2 [L,QSA]
</IfModule>
I think the regex should do exactly what I want, but in this case, seems that I'm wrong.
http://www.getnocms.com/forum/index.php?action=admin;area=manageboards;sa=newcat;df105e678e9b=e1a979a0631bd203b6794debc16ceced
Does anybody know how to do that correctly?
EDIT:
I use this
<IfModule mod_rewrite.c>
# Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteBase /
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^\/forum
RewriteRule (?!^forum(?:/.*|)$)^(.*)$ /index.php/$1 [L,NC,QSA]
</IfModule>
That roughly say:
If request is not pointed to file or directory or symbolic link, and it is not /forum then
If it is not started with forum, then point to /index.php/url.
It seems to be logical (well, we don't need RewriteCond %{REQUEST_URI} !^\/forum actually), but it still failed when accessing this url: http://www.getnocms.com/forum/index.php?action=admin;area=manageboards;sa=newcat;a5272d5=2fcf142818fc9df2aabb1364942a1d14
Maybe rewrite rule doesn't work with semicolon? I'm not sure.

Your rules are almost correct but some minor changes are needed. Replace your code with this:
<IfModule mod_rewrite.c>
# Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteBase /
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^forum(?:/.*|)$)^(.*)$ /index.php/$1 [L,NC]
</IfModule>

RewriteRule !^forum/ index.php/something_else_that_is_not_forum/blah_blah_blah
Anything that doesn't begin with...

You could also try:
RewriteCond %{REQUEST_URI} !^/forum
RewriteRule ^(.)$ http://domain.com/$1

Related

Redirecting from /index.php to / not working

The dynamic pages in my site are configured with following htaccess rule:
# Dynamic Pages
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
So the page is redirected from
http://example.com/?url=testpage
to
http://example.com/testpage/
But I also want to redirect /index.php to / (root). So when someone enters
http://example.com/index.php
in the browser address bar then it should go to
http://example.com/
To achieve this, I tried following rule:
#index.php to /
RewriteRule ^index\.php$ / [R=301]
Though this is working fine but it is affecting my previous htaccess rule. And the following URL
http://example.com/testpage/
automatically becomes
http://example.com/?url=testpage
When I remove this rule then previous dynamic page rule works fine. How can I keep both rules working in my htaccess file without conflicting each other?
My complete .htaccess file is:
Options +FollowSymlinks -MultiViews
#Enable mod rewrite
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
# Dynamic Pages
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
# index.php to /
RewriteRule ^index\.php$ / [R=301]
You can use these rules:
Options +FollowSymlinks -MultiViews
#Enable mod rewrite
RewriteEngine On
# index.php to /
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
# Dynamic Pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?url=$0 [QSA,L]
Important is to use THE_REQUEST in first rule so that you don't get rewrite loop. Clear your browser cache to test.

Simplifying the URL using ReWrite Rule

I have a blog which has a number fo categories in a categories.php page. And the original URL looks like this:
http://myname.com/articles.php?category=music
Which I have simplified to:
http://myname.com/articles/music
Using:
RewriteRule ^articles/([\w-]+)/?$ articles.php?cat=$1 [NC,L,QSA]
But is there a way of simplifying this further down to:
http://myname.com/music
--
UPDATE:
I've sorted out the original problem but now I'm hitting another problem. My .htaccess is:
Options -MultiViews
DirectoryIndex articles.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ articles.php?cat=$1 [NC,L,QSA]
RewriteRule ^([\w-]+)/([0-9]+)/?$ articles.php?cat=$1&currentpage=$2 [NC,L,QSA]
RewriteRule ^([0-9]+)/([\w-]+)/?$ article.php?id=$1&title=$2 [NC,L,QSA]
RewriteRule ^articles articles.php
RewriteRule ^categories categories.php
RewriteRule ^about about.php
RewriteRule ^feed feed.php
RewriteRule ^privacy privacy.php
When I visit http://myname.com/articles and http://myname.com/article... everything works fine, but when I go to http://myname.com/categories, http://myname.com/feed, etc I'm redirected back to the home page. Any ideas why this is?
You can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ articles.php?cat=$1 [L,QSA]

Apache .htaccess file not rewriting properly

I have an .htaccess file. It needs to rewrite URLs in two cases: for the MVC framework and in a more special case. I want to rewrite any requests to "/resources/newspaper" if the user doesn't have a cookie named "cfnw-hash". I've tried putting the code before the MVC code and after it. Doesn't work. It worked before switching over to the MVC framework, though I really don't have enough .htaccess knowledge to see if that's causing the issue. Here's code:
Options -Indexes
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteBase /
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_COOKIE} !cfnw-hash [NC]
RewriteRule \/resources\/newspaper.* http://www.example.com/error/401 [R=401,NC,L]
Combine your rules and I think you RewriteRule might not be matching because of the prepending / and also the cookie matching. Try this. Notice the cookie part that you update with your cookie.
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} !cookie_name=specific_value; [NC]
RewriteRule ^resources/newspaper/?(.*) http://www.example.com/error/401 [R=401,NC,L]
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
Have it this way:
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} !cfnw-hash [NC]
RewriteRule ^resources/newspaper /error/401 [R=401,NC,L]
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
Test this after clearing your browser cache.

Apply rewrite rule for all but all the files (recursive) in a subdirectory?

I have an .htaccess file in the root of the website that looks like this
RewriteRule ^some-blog-post-title/ http://website/read/flowers/a-new-title-for-this-post/ [R=301,L]
RewriteRule ^some-blog-post-title2/ http://website/read/flowers/a-new-title-for-this-post2/ [R=301,L]
<IfModule mod_rewrite.c>
RewriteEngine On
## Redirects for all pages except for files in wp-content to website/read
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/wp-content
RewriteRule ^(.*)$ http://website/read/$1 [L,QSA]
#RewriteRule ^http://website/read [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
My intent is to redirect people to the new blog post location if
they propose one of those special blog posts.
If that's not the case
then they should be redirected to http://website.com/read.
Nothing from http://website.com/wp-content/* should be redirected.
So far conditions 1 and 3 are being met. How can I meet condition 2?
Note: I want to redirect to /read even for content that exists to prevent any images, CSS, or javascript from working, except those in /wp-content
Make your first rule as:
## Redirects for all pages except for files in wp-content to website/read
RewriteRule !^wp-content/ http://website.com/read%{REQUEST_URI} [NC,L,R=302]

htaccess human readable

I have a site which already defined htaccess for making url nice
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
Options -indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php
Everything works fine (there is cms in the root folder).
Now I want to create a folder inside root and make another rules for it:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
Options -indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !tip [NC]
RewriteRule (.*) index.php [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^tip/([^/]+)/? /tip/?id=$1
So I want to redirect everything from url: .../tip/?id=N -> ../tip/N
It seems to work fine, id is passed, data is loaded BUT. All the urls are wrong inside site (javascript, css). They aren't loaded.
Look at: http://wincode.org/tip/3
For example, code: <script defer src="js/filtrify.js"></script> produces: http://wincode.org/tip/js/filtrify.js but if you will try to load it in another tab, it will pass js/filtrify.js as id argument, I think. How to fix this?
A. Change your .htaccess code to this in $DOCUMENT_ROOT/.htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
Options -indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^tip(/.*|)$)^.*$ /index.php [L,NC]
B. Change your .htaccess code to this in $DOCUMENT_ROOT/tip/.htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /tip
Options -indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/?$ /tip/?id=$1 [L,QSA]
I just improved little bit anubhava reply, this one is based on root folder example, but you can use same approach for other dir that you need.
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Options -indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^tip(/.*|)$)^.*$ /index.php [L,NC]
</IfModule>
<IfModule !mod_rewrite.c>
# Mod_rewrite not installed, redirect all 404 errors to index.php.
ErrorDocument 404 index.php
</IfModule>