getting htaccess rewrite rules to rewrite only specific urls - apache

I only want to rewrite this specific url:
http://www.website.co.uk/php/project.php?project=anythingHere
to
http://www.website.co.uk/php/project/anythingHere
this works fine but when I go to http://www.website.co.uk/php/project/anythingHere
it has changed all the links as well (style sheets, javascript, hyperlinks... things I don't want to change). for example:
http://www.website.co.uk/php/index.php
to
http://www.website.co.uk/php/project/index.php
This is what I have already
RewriteEngine On
RewriteCond %{HTTP_HOST} ^website.com\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.website.com\.co\.uk$
RewriteRule ^clients$ "http\:\/\/website.com\.co\.uk\/billing\/index\.php" [R=301]
RewriteRule ^project/([^/]*)$ /php/project.php?project=$1

Can you try this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?website\.co\.uk$ [NC]
RewriteRule ^clients/?$ http://website.co.uk/billing/index.php [NC,L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(php/project)\.php\?project=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(php/project)/([^/]+)/?$ /$1.php?project=$2 [L,QSA,NC]

Related

Using htaccess to redirect all pages but one to another domain

My htaccess is as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.es$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.es$
RewriteRule ^(.*)$ http://www.domain.com/es [L,R=301]
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?section=$1 [QSA,L]
Basically it's function is to redirect all domain alias (i.e. domain.mobi) to domain.com, except domain.es, that will be redirected to domain.com/es. Then there's another rewriterule that appends the query string (so in the background, domain.com/test becomes domain.com/index.php?section=test)
My problem is that now I need to exclude a url (www.domain.es/landing) from the redirection (so it stays in domain.es), and I can't make it work. I've tried adding this condition to the first two rules to exclude the page:
RewriteCond %{REQUEST_URI} !^/landing$
but then it goes to www.domain.com/es?section=landing (not only still redirects to domain.com; the query string appears in the browser bar). Any ideas?
You can use these rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.es$ [NC]
RewriteCond %{THE_REQUEST} !/landing[\s?] [NC]
RewriteRule ^(.*)$ http://www.domain.com/es/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{THE_REQUEST} !/landing[\s?] [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?section=$1 [QSA,L]

Change htaccess for subdomains

I have a site that works as a Romanian dictionary. Let's call it www.mysite.ro. I managed to do all the redirection for the main site (mysite.ro -> www.mysite.ro, mysite.ro/index.php -> www.mysite.ro) and also for the parameters through this topic of stackoverflow.
Now I would like to include an english dictionary. For this I have created a subdomain: english.mysite.ro. Now I would like to create the following redirects:
english.mysite.ro/index.php should redirect to english.mysite.ro
english.mysite.ro/index.php?cuvant=strong&form=care-incepe-cu ->
english.mysite.ro/care-incepe-cu/strong
My current .htaccess file is:
RewriteRule ^declinare/([^/]+)/? declinare.php?cuvant=$1 [L,NC,QSA]
RewriteRule ^conjugare/([^/]+)/? conjugare.php?cuvant=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]*)/?$ definitie.php?forma=$1&cuvant=$2 [L,QSA]
RewriteCond %{HTTP_HOST} ^toatecuvintele\.ro$ [OR]
RewriteCond %{HTTP_HOST} ^www\.toatecuvintele\.ro$
RewriteRule ^index\.php$ "http\:\/\/www\.toatecuvintele\.ro\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^toatecuvintele.ro$
RewriteRule ^/?$ "http\:\/\/www\.toatecuvintele\.ro\/" [R=301,L]
To this, I tried to add the following line, for my first question, but with no success:
RewriteCond %{HTTP_HOST} ^english.toatecuvintele.ro$
RewriteRule ^index\.php$ "http\:\/\/english\.toatecuvintele\.ro\/" [R=301,L]
Any ideas would just be great!
Thank you

Rewrite rule doesn't change the browser url

I have the following problem:
current url:
mysite.com/Category.php?Category=Category name
Want it to show up in the browser as follows:
mysite.com/Category name
I have the following in my .htaccess file
RewriteRule ^([a-zA-Z0-9_-]+)$ Category.php?Category=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ Category.php?Category=$1
but this doesn't seem to do anything
Thanks for looking
Current rules:
rewriteengine on
rewritecond %{HTTP_HOST} ^www.mysite.co.uk$ [OR]
rewritecond %{HTTP_HOST} ^musite1.co.uk$
rewriterule ^(.*)$ "http\:\/\/mysite\.co\.uk\/$1" [R=301,L] #5311a623b538f
rewritecond %{HTTP_HOST} ^www.mysite.co.uk$ [OR]
rewritecond %{HTTP_HOST} ^mysite.co.uk$
rewriterule ^Pianoshop\/detailed_page\.php(.*)$ "http\:\/\/mysite\.co\.uk\/product\.php$1" [R=301,L] #5310700a7e477
rewritecond %{HTTP_HOST} ^www.mysite.co.uk$ [OR]
rewritecond %{HTTP_HOST} ^mysite.co.uk$
rewriterule ^Pianoshop\/index_blog\.php(.*)$ "http\:\/\/mysite\.co\.uk\/news\.php$1" [R=301,L] #53
You need these 2 rules:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+Category\.php\?Category=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ Category.php?Category=$1 [L,QSA]
Do you have mod rewrite enabled ?
Your .htaccess should look like this :
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)/?$ Category.php?Category=$1

Redirect www to non-www is loosing my other RewriteRule

I have this code in my, .htaccess file.
My url is fine foodzite.dk/vis/1234567
When i enter www.foodzite.dk/vis/1234567 i want it to redirect to foodzite.dk/vis/1234567
but i redirects to foodzite.dk/shop.php?url=1234567
How is it i cant combine the to Rewrites?
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^vis/([a-z0-9\-_]+)/?$ show.php?url=$1 [L,QSA]
RewriteRule ^postnr/([^/]*)$ postal.php?postal=$1 [NC,L]
RewriteRule ^by/([^/]*)$ city.php?city=$1 [NC,L]
RewriteCond %{HTTP_HOST} !^foodzite\.dk$ [NC]
RewriteRule (.*) http://foodzite.dk/$1 [R=301,L]
You need to have your redirect rule before your routing rule. Otherwise the URI gets routed to your php file, then the redirect rule gets applied and the URI is exposed:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^foodzite\.dk$ [NC]
RewriteRule (.*) http://foodzite.dk/$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^vis/([a-z0-9\-_]+)/?$ show.php?url=$1 [L,QSA]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^postnr/([^/]*)$ postal.php?postal=$1 [NC,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^by/([^/]*)$ city.php?city=$1 [NC,L]
Additionally, your conditions only get applied to the immediately following rewrite rule, so the 2nd and 3rd don't have the same conditions. (Unless you wanted it to be that way).
Try something like:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^foodzite.dk [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Apache .hatccess rewrites not working for legacy URLS

I'm trying to rewrite some legacy Joomla URLs on a site that's now using ExpressionEngine as its CMS but they're not working.
The ExpressionEngine URL rewrites, i.e. removing index.php from the URL work fine though.
This is what I've got:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
# This is the one that's not working
RewriteRule /index.php?option=com_chronocontact&Itemid=54 /contact/ [R=301,L]
# Force www
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
# Redirect index.php Requests
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteCond %{THE_REQUEST} ^GET
RewriteRule ^index\.php(.+) $1 [R=301,L]
# Standard ExpressionEngine Rewrite
RewriteCond %{REQUEST_URI} ^/
RewriteCond %{QUERY_STRING} ^(gclid=.*)
RewriteRule ^(.+) /index.php?/ [L,PT]
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(assets|css|images|tinymce|js|min|cms|themes|index\.php|admin\.php|favicon\.ico|index\.php|path\.php|php\.ini) [NC]
RewriteRule ^(.+) /index.php?/ [L]
</IfModule>
Can anyone spot what I'm doing wrong?
The first thing is the stray RewriteCond %{HTTPS} !=on that you have at the top. It looks like it belongs to the rule under it, as in:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
As far as the rule that you have commented that doesn't work, the ? is a reserved character for regular expressions, and your pattern actually says that the second p in /index.php is "optional". Additionally, you can't match against the query string in a rewrite rule, you need to use a rewrite condition and match against the %{QUERY_STRING} variable:
RewriteCond %{QUERY_STRING} ^option=com_chronocontact&Itemid=54$
RewriteRule ^(index.php)?$ /contact/? [R=301,L]
is probably more along the lines of what you're looking for.