I would like to have two different redirects; one redirection if a user accesses an index.php-file on my apache-server and one redirection after a user enters a particular url.
So my index.php file lies in "/client/frontend/questionnaire"-folder on my apache. If a user enters "www.test-domain.com" he or she should be redirected to the index.php - file on the server.
The second redirection should be processed if a user enters "www.test-domain.com/news". Then he or she should be redirected to "www.test-domain.com/client/frontend/app/index.php/article-one".
I managed to create the first rewrite rule like this:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^$ client/frontend/questionnaire/index.php [L]
But I do not know how to create the second rule.
Any help is very appreciated.
Thanks!
You have to use multiple RewriteCond+RewriteRule. See the mod_rewrite introduction and then the reference documentation (all is explain).
Just the RewriteRule actually rewrite the current request. To execute it, all RewriteCond before must be true.
Samples from the documentation:
RewriteCond %{REMOTE_ADDR} ^10\.2\.
RewriteRule (.*) http://intranet.example.com$1
RewriteCond %{REMOTE_HOST} ^host1 [OR]
RewriteCond %{REMOTE_HOST} ^host2 [OR]
RewriteCond %{REMOTE_HOST} ^host3
RewriteRule ...some special stuff for any of these hosts...
RewriteCond %{HTTP_USER_AGENT} (iPhone|Blackberry|Android)
RewriteRule ^/$ /homepage.mobile.html [L]
Related
I have a domain, where everything except image\css etc. are handled by a single php file. However after a reorganisation, alot of images have been moved from sub-domains to the main domain. So I'm looking for a way to redirect all image\css files to the main domain if they were originally on one of the sub-domains. My current code is
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.ico|\.bmp|\.css|\.ts|\.js)$
RewriteRule !^index\.php$ /index.php [L]
I've tried a couple of ways to redirect it, but I seem to break on of the existing rules, whatever I try.
Thanks
The final solution I came up with
RewriteEngine On
# Check the request isn't for the main domain
RewriteCond %{HTTP_HOST} !^domain\.com$
# Check the request is for a static resource
RewriteCond %{REQUEST_URI} \.(png|jpg|gif|jpeg|ico|bmp|css|ts|js)$
# Redirect to main domain
RewriteRule (.*) http://domain\.com/$1 [R=301,L]
# if the request isn't for index.php,
# (invisibly) redirect to index.php
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.ico|\.bmp|\.css|\.ts|\.js)$
RewriteRule !^index\.php$ /index.php [L]
For information only. As really the credit goes to jon for the answer I just tweaked it for my needs.
If the URLs in your HTML still point to the subdomains, you'll need to setup htaccess redirects on those subdomains, not on the main domain, as the requests will still be going to the subdomains.
For example in /var/www/vhosts/sub.domain.com/httpdocs/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(png|jpg|gif|jpeg|ico|bmp|css|ts|js)$
RewriteRule (.*) http://domain.com/$1 [R=301,L]
And in /var/www/vhosts/sub2.domain.com/httpdocs/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(png|jpg|gif|jpeg|ico|bmp|css|ts|js)$
RewriteRule (.*) http://domain.com/$1 [R=301,L]
UPDATE
Based on your comment that Apache handles each subdomain as if it were the main one, try this:
RewriteEngine On
# Check the request isn't for the main domain
RewriteCond %{HTTP_HOST} !(www\.)?domain\.com$
# Check the request is for a static resource
RewriteCond %{REQUEST_URI} \.(png|jpg|gif|jpeg|ico|bmp|css|ts|js)$
# Redirect to main domain
RewriteRule (.*) http://domain.com/$1 [R=301,L]
1) i am trying to 301 all non www's to www's (including files)
e.g. /subdirectory/ + /subdirectory/1.jpg (all possibilities with the www.)
2) i am trying to exclude ONLY but ALL .html and .php files from showing and would like only the non trailing version to be indexed, the rest to be 301's to my domain
e.g. /example.html or .php >> /example
e.g. /example.html or .php >> to not work + to not be indexed
e.g. /example to ONLY work and to ONLY be indexed (to avoid duplicate content)
3) i am trying to 301 all dead links and 404's to my domain
e.g. /deadlink or /deadlink.pdf >> 301'd to my domain, example.com
Here is the code i currently have, however i am not sure if it's 100% proper.
Can someone please reply with a validated syntax for these 3 tasks? Thanks.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]
OK, you've got three parts you're trying for here, and I'll address them all separately.
Before we begin, you only need to have one
RewriteEngine on
It only needs to be turned on once.
You should also set:
Options -MultiViews
as MultiViews can otherwise cause some weird issues here.
Redirect to WWW
Your current rules should work, but you can simplify them a little using the != prefix on RewriteCond:
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Removing .php and .html suffixes
You need two separate sets of rules here: one to remove suffixes from URLs, and another to readd them internally.
Let's do the second half first, since that's easier:
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
With this in place, though, removing suffixes is a little tricky without conflicting with the rule that goes ahead and tries to put them back on. The best approach I was able to come up with was:
RewriteCond %{THE_REQUEST} \.(html|php)
RewriteRule ^(.*)\.(html|php)$ $1 [R=301,L,NC]
But I'm open to suggestions.
301ing all dead links
Don't do that. If a link doesn't exist, it should return a page with a 404 status. If you want to redirect visitors to your home page from dead links, put a link in your 404 page.
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]
I'm at a total loss trying to integrate a mod_rewrite in my existing page. Currently, I am building a new site from scratch, and there i have some nice clean url's such as:
http://www.example.nl/nl/example
The old site, running Cms made simple, has some not-rewritten url's that would need to be redirected to the new pages. Those url's look like this:
http://www.example.nl/index.php?page=cake-and-pie&hl=nl_NL
But shorter versions of that like:
http://www.example.nl/index.php?page=cake-and-pie
also work.
It took me a while to figure out that url's with parameters cannot simply be redirected with "Redirect 301", like i'd normaly do. So i tried some online mod_rewrite generators like this and this, but the rules outputted by those result only in 404 errors, (the redirect doesn't work at all).
My .htaccess file current looks like this:
RewriteEngine On
RewriteBase /
# remove .php;
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
The old pages would seize to exist.
How do i redirect the old pages to the new ones?
Thanks.
EDIT
RewriteCond %{QUERY_STRING} =page=pie
RewriteRule ^index\.php$ /nl/? [L,R=301]
RewriteCond %{QUERY_STRING} =page=pie&hl=nl_NL
RewriteRule ^index\.php$ /nl/? [L,R=301]
Seems to do the trick. This is of course manual for every url, but i only have a few.
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)/([^/]+)/?$ index.php?page=$1&hl=$2
RewriteRule ^([^/]+)/?$ index.php?page=$1 [QSA]
I use the below mod rewrite code for urls like: www.site.com/play/543
RewriteEngine On
RewriteRule ^play/([^/]*)$ /index.php?play=$1 [L]
How can I expand on this so I can have a couple urls like www.site.com/contact and www.site.com/about
RewriteRule ^(play|contact|about)/([^/]*)$ /index.php?$1=$2 [L]
Might do the trick. Now requests to /play/foo points to /index.php?play=foo and /contact/bar points to /index.php?contact=bar and so on.
Edit, from comment "about and contact will never be set though."
Just use two rewrites then;
RewriteRule ^play/([^/]*)$ /index.php?play=$1 [L]
RewriteRule ^(contact|about)/?$ /index.php?a_variable_for_your_page=$1 [L]
The common way used by most of the PHP frameworks is just passing whatever the user requests except existing files (generally assets *.png, *.js *.css) and/or public directories to a PHP script and then interpreting the route on the PHP side.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d #if not an existing directory
RewriteCond %{REQUEST_FILENAME} !-f #if not an existing file
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] #pass query string to index.php as $_GET['url']
</IfModule>
On the PHP side it is very important to avoid things like this
$page = getPageFromUrl($_GET['url']);
include($page);
So be very careful when taking the user input and sanitize/filter to avoid the remote user to access non-public files on your web host.