htaccess rewriterule adds unwanted get parameter - apache

What I have in my htaccess file:
RewriteEngine on
# --- Remove index.php from URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule .* index.php/$0 [PT,L]
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^city/([^/]+)/?$ /uk/city/$1 [R=301,L]
What I like it to do is redirect example.com/city/london to example.com/uk/city/london
The weird thing is that it now redirects to example.com/uk/city/london?city/london so it seems it adds the part it needs to redirect as a get parameter to the new URL.
Also tried Redirect 301 /city/london http://www.example.com/uk/city/london but this gives the same result.

You need to keep R=301 (redirect) rule before internal routing rule:
RewriteEngine on
RewriteRule ^city/([^/]+)/?$ /uk/city/$1 [R=301,L]
# --- Remove index.php from URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule .* index.php/$0 [PT,L]
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Also clear your browser cache to test this to avoid hitting old 301 cache.

You can try something like this if the Redirect 301 did not work:
RewriteRule /city/london uk/city/london [L]
Here's an example with your other defined .htaccess rules:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule /city/london uk/city/london [L]
# --- Remove index.php from URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule .* index.php/$0 [PT,L]
RewriteRule ^(.*)$ index.php?/$1 [L]

Related

htaccess Redirect all except the specified URL

Cannot use (!) reverse operation, a redirect URL (http://ssvwv.com) will appear:
/1/3/ktrb
Will not display (index.html)
RewriteEngine on
RewriteCond %{REQUEST_URI} !^\/1\/(?:1|2|3)\/.*$
RewriteRule ^(.*)$ http://ssvwv.com/ [L,R=302,NE]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule . /index.html [L]
This should work for you:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(?:index\.html|1/[1-3]/.*)$ [NC]
RewriteRule ^ http://ssvwv.com/ [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.html [L]
Issue is that last rule is rewriting to /index.html and when mod_rewrite loops again URI becomes /index.html and RewriteCond %{REQUEST_URI} !^\/1\/(?:1|2|3)\/.*$ returns true thus redirecting to http://ssvwv.com/. That's why you need to skip /index.html from first rule as well.

How to remove index.php from www to non-www .htaccess redirects

When I go to
www.example.co/courses
I get redirected to
https://example.co/index.php/courses
I want to get redirected to
https://example.co/courses
What I have tried
Here is my .htaccess file at the moment:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.co [NC]
RewriteRule ^(.*)$ https://example.co/$1 [L,R=301]
</IfModule>
How do I remove the /index.php/ part of the redirected URL?
You've got your external redirects in the wrong place. They need to go before your internal rewrites (before your front controller). For example:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.twocan\.co [NC]
RewriteRule ^(.*)$ https://twocan.co/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If you have your external redirects after the internal rewrite then the request gets rewritten to /index.php/... which then gets turned into an external redirect (the rewrite process loops in .htaccess, so it only gets rewritten to index.php on the first loop).

htaccess issue in accessing the directory

My Blog link is http://www.example.com/blog when i am trying to access it it converts the url into http://www.example.com/blog/?ID=blog which is not correct. But when i put the backslash at the end of the blog it works fine but i don't want this back slash at the end
I try to change every thing inside htaccess file but failed.
Here is my htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ - [L]
RewriteRule ^products products.php
RewriteRule ^websitemap sitemap.php
RewriteRule ^aboutus aboutus.php
RewriteRule ^clients clients.php
RewriteRule ^shoppingcart inq.php
RewriteRule ^contactus contact.php
RewriteRule ^finished finished.php
RewriteRule ^product/([a-zA-Z0-9_-]+)$ product_large.php?ID=$1
RewriteRule ^([a-zA-Z0-9_-]+)$ category.php?ID=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ products.php?ID=$2&MID=$1
ErrorDocument 404 http://www.example.co.uk/error404.php
RewriteCond %{HTTP_HOST} ^example.co.uk$
RewriteRule ^/?$ "http\:\/\/www\.example\.co\.uk\/" [R=301,L]
Insert this rule after RewriteRule ^ - [L] to force a trailing slash:
DirectorySlash On
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^blog(/.*)?$ - [L,NC]
# your rest of the rules

TYPO3 breaks urls without WWW

The problem is that when I want to access my website like www.example.com/my/subpage all works great but when I try to access my website like this (without WWW) example.com/my/subpage TYPO3 breaks it down and redirect it to www.example.com/index.php/subpage.
The redirect from non WWW to WWW is correct but why does it destroy my url? Here is the .htaccess redirect:
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
For the "nice" urls I use the realURL extension. Any ideas?
Edit (more htaccess):
RewriteRule ^(typo3|t3lib|tslib|fileadmin|typo3conf|typo3temp|uploads|showpic\.php|favicon\.ico)/ - [L]
RewriteRule ^typo3$ - [L]
RewriteRule ^typo3/.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php
Keep your 301 rules before other internal rewrite rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301,NE]
RewriteRule ^(typo3|t3lib|tslib|fileadmin|typo3conf|typo3temp|uploads|showpic\.php|favicon\.ico)/ - [L]
RewriteRule ^typo3(/.*)?$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php [L]
Also test this in a new browser to avoid browser caching issues.

.htaccess redirect trouble

I'm having trouble with (what should be) a simple .htaccess redirect. I need to changed any url that has "for_homes" into "for_home".
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
However when I go to any pages with for_homes I get a 404.
Rewrite Rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
</IfModule>
The problem is a preceeding rule is rewriting to index.php. In this block here:
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
### This rule rewrites "for_homes" to "index.php?/for_homes"
RewriteRule ^(.*)$ index.php?/$1 [L]
### These rules never get applied
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
So perhaps after the RewriteCond %{REQUEST_FILENAME} !-d, add conditions to exclude the later 2 rewrites, So that it looks like this:
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/for_home(s)?
RewriteCond %{REQUEST_URI} !^/for_business(es)?
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
If it's your intention to have index.php handle for_home and for_businesses, so that when someone enters http://domain.com/for_homes/stuff in their browser's address bar, the browser gets redirected to http://domain.com/for_home/stuff, then it gets internally rewritten to /index.php?/for_home/stuff so that index.php can handle the request, you simply need to move the 301 redirects before the index.php rewrite:
### Redirect first
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Perhaps you are just missing RewriteEngine On
RewriteEngine On
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]