.htaccess redirect all but not two - .htaccess

I'm trying to redirect like this using .htaccess:
* -> index.html
/affiliate -> affiliate.html
/ref/* -> ref.html
But the problem is, that all links are redirected to index, how I can exclude several links from this rule?
I have this code for now:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>

With your shown samples/attempts please try following htaccess rules file.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules for uris which starts from affiliate rewrite to affiliate.html here..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^affiliate/?$ affiliate.html [QSA,NC,L]
##Rules for uris which starts from ref rewrite it to ref.html here..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ref ref.html [QSA,NC,L]
##Rules for rest of the non-existing pages to rewrite to index.html here...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.html [L]

Have your rules like this instead:
Options -MultiViews
DirectoryIndex index.html
RewriteEngine On
# Prevent further processing if request maps to a static resource
RewriteRule ^((index|affiliate|ref)\.html)?$ - [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
# /affiliate -> affiliate.html
RewriteRule ^affiliate$ affiliate.html [L]
# /ref/* -> ref.html
RewriteRule ^ref/ ref.html [L]
# * -> index.html
RewriteRule . index.html [L]
You do not need the RewriteBase directive here. (Or the <IfModule> wrapper.)

Related

Url Rewrite - Add Query String Too 999

DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
RewriteRule ^tags tags.php [L,NC]
</IfModule>
i want to add new page .but write page not found. why? please help me.
RewriteRule ^tags tags.php [L,NC] // i want this www.example.com/tags open tags.php ?
You need to reorder your htaccess rules like this :
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteRule ^tags/?$ tags.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
The problem is that, your rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
Rewrites any request (not a true file/dir) to index.php. since /tags is not an existent file on your directory, so a request for http://example.com/tags was being rewritten to /index.php and the last rule(s) never executed.

Redirecting referrer URLs based on new site structure

So I'm trying to redirect incoming connections based on the referrer URL. So for instance if someone was trying to visit:
blog.example.com/category/fancy-stuff
They are already getting redirected to example.com but I want to redirect them to:
example.com/news/category/fancy-stuff
Right now I have this for my htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_REFERRER} blog.example.com/category/* [NC]
RewriteRule ^ example.com/news/category/*? [R=301,L]
</IfModule>
Is there a way to use variables or something to just append the proper category to the new URL?
Reorder your Rewrite rules :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERRER} ^blog.example.com/category/.*$ [NC]
RewriteRule ^(.*)$ http://example.com/news/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

.htaccess don't go to folder if slash on end

I have site where
site.com/index.php?page=test (include file test.php from pages folder into index.php)
is replaced by site.com/test
Goal:
If I go to site.com/index.php?page=forum/new_enrty
I want ot make url site.com/forum/new-entry
Problem:
if I open site.com/forum/new-entry it directs me to forum directory to new-entry file directly and not include new-entry into index.php
So far I got this:
<IfModule mod_rewrite.c>
RewriteEngine on
#RewriteBase /cms/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php [L]
</IfModule>
Replace your code with this:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+?)/?$ index.php?page=$1 [L,QSA]
Try this one:
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]

Rewrite directory to subdomain

I have a page with two subdomains: cw.sidenote.hu and cellwars.sidenote.hu, both point to sidenote.hu/cellwars/.
What I'd like to achieve that when I visit cw.sidenote.hu or cellwars.sidenote.hu that the URL change to/stay in cellwars.sidenote.hu format, and don't change to sidenote.hu/cellwars/.
This is what I currently have in my .htaccess file:
RewriteEngine on
# Some hosts require a rewritebase rule, if so, uncomment the RewriteBase line below. If you are running from a subdirectory, your rewritebase should match the name of the path to where stacey is stored.
# ie. if in a folder named 'stacey', RewriteBase /stacey
#RewriteBase /cellwars
ErrorDocument 404 /404.html
# Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !public/
RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
RewriteRule (.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ [L]
# Add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.)
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ([^/]+)$ $1/ [L]
# Rewrite any calls to /* or /app to the index.php file
RewriteCond %{REQUEST_URI} /app/$
RewriteRule ^app/ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?$1 [L]
# Rewrite any file calls to the public directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(.+)$ public/$1 [L]
These are the rewrite rules I'v tried to use, but it seems this gets the site into an infinite rewrite loop:
# cw.sidenote.hu -> cellwars.sidenote.hu
RewriteCond %{HTTP_HOST} ^cw.sidenote.hu$
RewriteRule ^(.*)$ http://cellwars.sidenote.hu/ [R=301,L]
# sidenote.hu/cellwars/* -> cellwars.sidenote.hu/*
RewriteCond %{REQUEST_URI} ^/cellwars/?(.*)$
RewriteRule ^(.*)$ http://cellwars.sidenote.hu/%1 [R=301,L]
This is what I got back from Chrome:
This webpage has a redirect loop
The webpage at http://cellwars.sidenote.hu/ has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.
Here are some suggestions:
Reload this webpage later.
Learn more about this problem.
Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.
Could anyone help me? Thanks in advance!
Try replacing your htaccess file content with this (I didn't change anything beyond the ErrorDocument 404 /404.html line) :
RewriteEngine on
# Some hosts require a rewritebase rule, if so, uncomment the RewriteBase line below. If you are running from a subdirectory, your rewritebase should match the name of the path to where stacey is stored.
# ie. if in a folder named 'stacey', RewriteBase /stacey
RewriteBase /
# cw.sidenote.hu -> cellwars.sidenote.hu
RewriteCond %{HTTP_HOST} ^cw\.sidenote\.hu$
RewriteRule ^(.*)$ http://cellwars.sidenote.hu/$1 [R=301,L]
# sidenote.hu/cellwars/* -> cellwars.sidenote.hu/*
RewriteCond %{HTTP_HOST} ^(www\.)?sidenote\.hu$
RewriteRule ^cellwars/?(.*)$ http://cellwars.sidenote.hu/$1 [R=301,L]
ErrorDocument 404 /404.html
# Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !public/
RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
RewriteRule (.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ [L]
# Add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.)
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ([^/]+)$ $1/ [L]
# Rewrite any calls to /* or /app to the index.php file
RewriteCond %{REQUEST_URI} /app/$
RewriteRule ^app/ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?$1 [L]
# Rewrite any file calls to the public directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(.+)$ public/$1 [L]

Is there a way for making the re-write rule exclude some folders?

I have this in my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I want this rule to NOT run if the url has /Users or /Forums in the url.
How can I do this?
Update
What about file extensions?
Just add another condition that excludes such URL paths:
RewriteCond %{REQUEST_URI} !^/(Users|Forums)(/|$)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Resources