How to block URLs different than main domain with Apache? - apache

I want to block all request (or return 404 apaches error page) in my Wordpress different than the main url.
Example:
https://myurl.com this must work
https://myurl.com/fksdofiewoq this musnt work
https://myurl.com/wp-admin must work
myurl.com/mainpost must work clicking in the post in the main page.
Is there any posibility?

I could solve it with this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !/index.php
RewriteCond %{REQUEST_URI} !/wp-admin
RewriteCond %{REQUEST_URI} !/wp-login.php
RewriteCond %{REQUEST_URI} !/hello-world/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(pdf|jpeg|css|gif|ico|jpg|js|png|swf|txt|svg|woff|ttf|eot)$
RewriteRule . / [R=404,L]
This can filter that uris and block the rest of urls

Related

htaccess error on redirect - too many redirects

I'm trying to redirect all requests from one domain (domain.co.in) to another domain (domain.info.in). I've tried Rewrite directives and Redirect directive in htaccess, but getting too many redirects error in browser. Below is the configuration I'm trying to implement.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.in [NC]
RewriteRule ^(.*)$ index.php/$1
RewriteRule ^(.*)$ http://domain.info.in/$1 [R,L]
My actual working htaccess configuration is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php/$1
Using this, I'm able to redirect all requests to index.php and working fine. But when I add domain rewrite rules, I'm getting the redirect error. I've tried Redirect directive also,
Redirect 302 / http://domain.info.in/index.php/$1, but same error.
I tried the Fiddler tool mentioned in this post, Tips for debugging .htaccess rewrite rules. There it is working good.
Actually, I want to redirect all requests (www.domain.co.in, domain.co.in, www.domain.info.in) to domain.info.in
Any suggestions on this?
As per #CBroe's suggestion, I've updated the configuration and it works. As he said,
RewriteConds always affect the directly following rule.
And I've also added negation to the checking to redirect all other requests.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain.co.in$ [NC]
RewriteRule ^(.*)$ http://domain.info.in/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php/$1 [L]

Apache redirect username to profile page conflict

I am trying to write redirect rules which will redirect usernames to profiles pages. The problem is I don't want to use extra parameter to determine that current url is for user. If I use example.com/user/test pattern then there is no problem. But I want to use example.com/test pattern.
The problem is also other pages uses this pattern beside user profile. For ex. example.com/messages. But these other pages are only a few. Hard coded exceptions should be fine on redirect rules.
Here is what I use for redirecting.
RewriteRule ^([^/]*)/$ /index.php?page=profile&username=$1 [L]
But for urls like /messages/, /login/, /settings/ I want to use this rule.
RewriteRule ^([^/]*)/$ /index.php?page=$1 [L]
Another problem when I remove ending slash from first redirect rule
RewriteRule ^([^/]*)$ /index.php?page=profile&username=$1 [L]
Like this, redirect loop happens. Actually I don't want to have ending slash but I fixed temporarily by adding it.
I am not sure if this affects other redirect rules but also I want to redirect index.php to ``.
To sum everything up I am listing what I am trying to do.
example.com/index.php => example.com
example.com/index.php?page=messages => example.com/messages
example.com/index.php?page=options => example.com/options
example.com/index.php?page=profile&username=test => example.com/test
Thanks.
Found it myself!
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/404
RewriteCond %{REQUEST_URI} !^/messages
RewriteCond %{REQUEST_URI} !^/login
RewriteCond %{REQUEST_URI} !^/logout
RewriteCond %{REQUEST_URI} !^/options
RewriteCond %{REQUEST_URI} !^/deposit
RewriteCond %{REQUEST_URI} !^/profile
RewriteRule ^(.*)$ index.php?page=view-profile&username=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]

Apache rewrite "domain.com/folder" to "folder.domain.com" with keeping redirection from "folder.domain.com" to "domain.com/folder"

I want folder.domain.com to point at domain.com/folder
and
I want domain.com/folder to be rewritten in the URL of page in the browser to folder.domain.com.
In both cases I want to see an URL like this folder.domain.com.
Tried:
Redirect 301 /subdomain http://subdomain.example.com
Neither work:
RewriteCond %{THE_REQUEST} ^GET\ /ucp/profile\.php?([^=]+)=(\S+) [NC]
RewriteRule ^ucp/profile\.php$ /ucp/%1/%2? [R=301,L,NC]
# Now, deal with internal rewrites (which will not cause redirection):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ucp/([^/]+)/([^/]+)/?$ /ucp/profile.php?$1=$2 [NC,L]
enter code here
And many many more.
Is it even achievable?

Use mod_rewrite to rewrite get arguments with slashes

I have a website which has a few standalone webpages (like signup.php) and a main PHP file that displays the rest of the pages using a GET parameter.
I wish to rewrite HTTP requests to the server so that a request like:
http://example.com/website/settings/ would display the page
http://example.com/website/index.php?page=settings, without affecting all the other pages.
I'm quite new to mod_rewrite and all of my attempts have resulted in either a 404 or 500 response code. What is the correct way to write the RewriteCond and RewriteRule?
Add these rules in the htaccess file in the document root (where the website folder is in):
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+website/(?:index\.php|)\?page=([^&]+)/(&|\ |$)
RewriteRule ^ /website/%1/ [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^website/(.+)/$ /website/index.php?page=$1 [L,QSA]
Or you could instead put these rules in the website folder if that makes more sense:
RewriteEngine On
RewriteBase /website/
RewriteCond %{THE_REQUEST} \ /+website/(?:index\.php|)\?page=([^&]+)/(&|\ |$)
RewriteRule ^ %1/ [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ index.php?page=$1 [L,QSA]

mod_rewrite Rules Giving 404

I'm sure this has been answered, I've been reading for a few hours now and am getting nowhere. I need this to work tonight and my brain is hurting, so I'm asking the the wonderful internet for help.
I'm trying to run all my pages through index_new.php (it all made sense when I decided to do that, I swear). I have two types of pages, static and dynamic. The dynamic pages are all the same kind of page, but with different data based on the database (MySQL). I'm trying to make these rewrites
/about => index_new.php?page=about
/installations => index_new.php?page=about
/installations/{site_id} => index_new.php?page=site&siteID={site_id}
(I'd love if about could be generic, but I don't want to push my luck) My .htaccess file is:
# enable rewrite
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# rewrite all physical existing file or folder
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/statics/" [OR]
RewriteCond ${REQUEST_URI} "/ajax/"
# rewrite rules
RewriteRule ^about index_new.php?page=about
RewriteRule ^/installations index_new.php?page=list
RewriteRule ^/installations/(.*) index_new.php?page=site&id=$1
When I try to go to /about or any other page, I get HTTP 404. As a note, my root is http://localhost/~user/public_html/new and the .htaccess file is there.
Provided:
The conditions in the question have to be met for each rule. (They are repeated as they are valid only for the next rewrite rule).
The .htaccess file is located at root.
You may try this instead of the rule-set in your question:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /about/?$ [NC]
RewriteRule .* /index_new.php?page=about [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /installations/?$ [NC]
RewriteRule .* /index_new.php?page=list [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /installations/([^/]+)/? [NC]
RewriteRule .* /index_new.php?page=site&id=%1 [NC,L]
Maps silently:
http://example.com/about to
http://example.com/index_new.php?page=about
http://example.com/installations to
http://example.com/index_new.php?page=about
http://example.com/installations/Anything to
http://example.com/index_new.php?page=site&Anything
For permanent and visible redirection, replace [NC,L] with [R=301,NC,L].
NOTE:
These conditions in the question were not used as their purpose is not clear:
RewriteCond %{REQUEST_URI} "/statics/" [OR]
RewriteCond ${REQUEST_URI} "/ajax/"
To include them for one or several rules, try this:
# For NOT condition, include the next line before the corresponding rewrite rule:
RewriteCond %{REQUEST_URI} !(statics|ajax) [NC]
# For positive condition, include the next line before the corresponding rewrite rule:
RewriteCond %{REQUEST_URI} (statics|ajax) [NC]