mod_rewrite disallow call of subfolder - mod-rewrite

I am using 2 Domains like http://doma.in and http://domain.com
The shortning URL calls only generated URL's from my Script. All other calls from the shortning URL should be forwarded to the Main Domain. Thats why I have this small mod_rewrite Rule.
RewriteCond %{HTTP_HOST} ^doma.in [NC]
RewriteRule ^/?$ http://www.domain.com/$1 [R=301,L]
The Question
I want to disallow calling subfolders from the shortning URL. Because the shortning URL can also generate Custom URLs like
http://www.nokia.com -> http://doma.in/nokia
If I would have a subfolder called "nokia" than it will not be forwarded to the target. Instead of to forward it is calling the subfolder.
To forward URLs over mod_rewrite is this Rule used.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-x
RewriteRule ^(.*) /redirect.php?id=$1 [L]

Your question is a little unclear.
If you want the redirect to execute for /nokia even if you have a directory called /nokia, you need to remove the line:
RewriteCond %{REQUEST_FILENAME} !-d
What that condition says is "If the file path of the request is an existing directory, do not continue."

Related

mod_rewrite forward shortend URL

I am looking for a way to create a short URL path for a longer URL on my page
the long url is: domain.com/tagcloud/user.html?t=1234ABCD
i would like to offer a short version of the URL to easy access it:
domain.com/t/1234ABCD
I tried a few examples but I just don't get it how I could forward these rules.
RewriteRule ^(.*)/t/$ /tagcloud/user.html?t=$1 [L]
I am also using MODX so they already use rules.
in addition my htaccess file
RewriteEngine On
RewriteBase /
# Always use www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I must keep the code snippets above in my htaccess file. The first one simply forwards http://domain.com requests to www.domain.com
The friendly URLs part is needed to translate the internal IDs of my CMS with the alias of the URL. This feature must remain because the entire site cannot be influencted by the changes I try to make in htaccess...
I simply would like to add a listener that only if the URL matches www.domain.com/t/abcd1234
Therefore I need something that identifies the www.domain.com/t/ URL
your help is much appreciated
Try this:
RewriteCond %{REQUEST_URI} ^/t/.*
RewriteRule ^t/(.*)$ /tagcloud/user.html?t=$1 [R=301,L]

mod_rewrite - modify URL and continue with processing

This is excerpt from my .htaccess:
RewriteRule ^([0-9]+)x([0-9]+)$ images/$1x$2.png
# front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(pdf|js|ico|gif|jpg|css|rar|zip|tar\.gz)$ index.php [L]
What I am trying to achieve: When user goes to URL example.com/200x100, I want to internally rewrite the URL to example.com/images/200x100.png, so the application (front controller) sees the REQUEST_URI as /images/200x100.png, not just 200x100. If the file /images/200x100.png exists, the application should not be launched at all.

What is the solution of making dynamic subdomain like links

I have a shopping mall site built with Magento. The url is like http://exampleshopping.com
Now I have requirement where each of my users will have a vanity URL like the following -
http://abc.exampleshopping.com
http://xyz.exampleshopping.com
In the above example abc, xyz are the user names.
How is this possible without creating a sub domain from the control panel. I need this to be created automatically when a user registers.
Create an .htaccess file with the following code to build a dynamic username as subdomain.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.exampleshopping\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.exampleshopping\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?username=%1 [L]
All visits to http://username.exampleshopping.com will redirect to www.exampleshopping.com/userpage.php?username=subdomain. User only see http://username.exampleshopping.com in the address bar.
Reference : http://www.panolee.com
Use the following code in your main .htaccess file (for the www subdomain) - or in the Apache Vhost config.
Replace sonassi.com and paths to suit.
RewriteEngine On
RewriteCond %{HTTP_HOST} !www\.sonassi\.com
RewriteCond %{HTTP_HOST} ([^\.]+)\.sonassi\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?user=%1$1 [L]
You need to also create a wildcard DNS A record, if you are using cPanel, you would use
*.sonassi.com => my.ip.add.ress
Or, if you are using TinyDNS - use the following syntax
+*.sonassi.com:my.ip.add.ress:86400
Using a htaccess regex url Rewrite will work for you.

Rewrite rules confused

I am struggling to achieve this simple thing...
I have some static pages which should be like
www.domain.com/profile etc..
The problem is how to write the rewrite rules in order to ..
There would be some fixed rewrites
like /home
I want every file that exists not to be rewritten
www.domain.com/test.php should go to
test.php
Lastly if it is not found i want it to be redirected to static.php?_.....
RewriteRule ^/home/?$ /index.php?__i18n_language=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]+)/?$ /static.php?__i18n_language=$1
This works ok but if i type index.php or test.php or even the mach from other redirection it gets me in static.php...
Please help!
According to your description you can use these rules:
# stop rewriting process if request can be mapped onto existing file
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteRule ^ - [L]
# rewrite known paths /home, /foo, /bar, etc.
RewriteRule ^/(home|foo|bar|…)$ /index.php [L]
# rewrite any other path
RewriteRule ^ /static.php [L]
I haven't used this in a long time, but it's something I found, that should help. It is part of an old script that generates .httaccess files for redirecting from /usr/share/doc only when the doc isn't found:
The rule is "Check, and if the target url exists, then leave":
RewriteEngine On
RewriteBase /doc/User_Documents
### If the directory already exists, then leave
### We're just redirecting request when the directory exists but has been renamed.
RewriteCond %{REQUEST_FILENAME} User_Documents/([^/]+-[0-9][^/]*)
RewriteCond $PWD/%1 -d
RewriteRule .* - [L]
It's the [L] that means leave if one of the conditions is matched. In the old script, there are hundreds of generated rules (after [L]) that are skipped, because one of the conditions matched. In your case you would skip the rest of the rules when the target %{REQUEST_FILENAME} is found.
So, I suggest, before the redirection rule:
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]

Rewrite URLs for static content

I have problems with static files url rewriting in current .htaccess setup on apache2.
My app structure is:
/siteroot
/siteroot/app
/siteroot/lib
/siteroot/...
/siteroot/public <- all the static files (images, js, etc.) stored here
/siteroot/index.php
/siteroot/.htaccess
So, i need to rewrite url like /css/style.css to /public/css/style.css. I did that in really simple way, but when the file is not found it causing 10 internal redirects, which is bad. I need somehow to return 404 code if file not found, or just pass it to the next rule. And i dont have any access to site configuration file. Only .htaccess.
The reason why i`m asking this question is that the site was running on nginx and i need to rebuild the same configuration on apache.
Here is my .htaccess file.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.+\.(jpg|jpeg|gif|png|ico|css|js|swf)$ /public/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Test if a redirect is reasonable:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/public/$0 -f
RewriteRule ^.+\.(jpg|jpeg|gif|png|ico|css|js|swf)$ /public/$0 [L]
If the number of prefixes is limited, you could add another group to your regex:
RewriteRule ^(css|js|images|etc)/.+\.(jpg|jpeg|gif|png|ico|css|js|swf)$ /public/$0 [L]

Resources