.htaccess redirecting errors - .htaccess

First off I am using the Codeigniter Framework so this issue is a workaround the way CI process URLs along with the current redirects I have set up using mod_rewrite.
I am trying to get a URL like this /?gclid=somestringgoeshere to redirect to /index.php?/home/gclid/somestringgoeshere.
The current .htaccess I have set is below
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6_$7 [L]
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6 [L]
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5 [L]
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4 [L]
RewriteRule ^([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3 [L]
RewriteRule ^([^_]+)-([^.]+)$ index.php?/$1_$2 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# This last condition enables access to the images and css folders, and the robots.txt file
# Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
I am trying to use the following code right above the first set up rewrite conditions and rule's to catch it before it try's anything else
RewriteRule ^?gclid=(.*)$ index.php?/home/gclid/$1 [L]
and
RewriteRule ^\?gclid=(.*)$ index.php?/home/gclid/$1 [L]
and
RewriteRule ^/?gclid=(.*)$ index.php?/home/gclid/$1 [L]
All either don't show the correct page or come up with a 500 internal error.

The URI’s query can only be tested with the RewriteCond directive:
RewriteCond %{QUERY_STRING} ^gclid=(.*)
RewriteRule ^$ index.php?/home/gclid/%1 [L]
Or more general (will consider further query parameters):
RewriteCond %{QUERY_STRING} ^([^&]*&)*gclid=([^&]*)
RewriteRule ^$ index.php?/home/gclid/%2 [L]
Oh, by the way: RewriteCond directives only correspond to the first following RewriteRule directive.

Related

how to mask part of URL after doing a mod_rewrite

I have several client domains I would like to point to my droplet(I have a vue app here) and serve the right path depending on the domain being called.
eg:
domainA.com should go to mysite.com/path-a
domainB.com should go to mysite.com/path-b
I have tried to rewrite the URL using .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} =domainA
RewriteCond %{REQUEST_URI} =/
RewriteRule ^ %{HOST_NAME}/uri-a [R=301,L]
# Handle subsequent routes
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
However, I can't seem to be able to remove the URI and just show the client domain name.
Would also like to make it dynamic so I don't have to write the same rule for each client domain
Could you please try following, based on your shown samples, please clear your browser cache before testing your URLs.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^/?$ http://%{HTTP_HOST}/path-a [R=301,L]
RewriteCond %{HTTP_HOST} ^domainB\.com$ [NC]
RewriteRule ^/?$ http://%{HTTP_HOST}/path-b [R=301,L]
# Handle subsequent routes
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
You may try this code in your root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainA\.com$ [NC]
RewriteRule ^$ path-a [L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainB\.com$ [NC]
RewriteRule ^$ path-b [L]
# Handle subsequent routes
RewriteRule ^index\.html$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.html [L]
Make sure to test from a new browser or after clearing your browser cache.

generic solution for Rewriteurl for all URL's [duplicate]

My website runs a script called -> WSS wallpaper script
My Problem -> I have been trying to force remove or add trailing slash to the end of my URL to prevent duplicated content and also to clean up my URLs.
I have tried all sorts and tried everything I could think of and loads from the interwebs but no such luck yet! It might be a quick fix but I have looked at it so much I am probably blind to something dead obvious.
So I present you with all my .htaccess code:
DirectoryIndex index.php
RewriteEngine on
RewriteRule ^download/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+) image.php?id=$1&width=$2&height=$3&cropratio=$4&download=1 [L]
RewriteRule ^file/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+) image.php?id=$1&width=$2&height=$3&cropratio=$4 [L]
RewriteRule ^preview/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+) wallpaper_preview.php?id=$1&width=$2&height=$3&name=$4 [L]
RewriteRule ^thumbnail/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/]+) image.php?wallpaper_id=$1&width=$2&height=$3&cropratio=$4&align=$5&valign=$6&file=$7 [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+)/p([0-9]+) index.php?task=category&id=$1&name=$2&page=$3 [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+)/([0-9a-zA-Z?-]+)/p([0-9]+) index.php?task=category&id=$1&name=$2&sortby=$3&page=$4 [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+)/([0-9a-zA-Z?-]+)-([0-9]+) index.php?task=category&id=$1&sortby=$3&page=$4 [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+) index.php?task=category&id=$1&name=$2 [L]
RewriteRule ^tag/([^/\.]+)/([0-9a-zA-Z?-]+)/([0-9]+) index.php?task=tag&t=$1&sortby=$2&page=$3 [L]
RewriteRule ^tag/([^/\.]+) index.php?task=tag&t=$1 [L]
RewriteRule ^profile/([0-9]+)?/([^/\.]+) index.php?task=profile&id=$1&name=$2 [L]
RewriteRule ^profile/comments/([0-9]+)?/([^/\.]+) index.php?task=users_comments&id=$1&name=$2 [L]
RewriteRule ^page/([0-9]+) index.php?task=view_page&id=$1 [L]
RewriteRule ^register index.php?task=register [L]
RewriteRule ^lost-password index.php?task=lost_pass [L]
RewriteRule ^links index.php?task=links [L]
RewriteRule ^news/item/([0-9]+)/([^/\.]+) index.php?task=news&id=$1 [L]
RewriteRule ^news/page([0-9]+) index.php?task=news&page=$1 [L]
RewriteRule ^members/([^/\.]+)-([^/\.]+)/page([0-9]+)? index.php?task=member_list&sort=$1&order=$2&page=$3 [L]
RewriteRule ^members index.php?task=member_list [L]
RewriteRule ^messages index.php?task=messages [L]
RewriteRule ^submit index.php?task=submit [L]
RewriteRule ^search/([^/\.]+) index.php?task=search&q=$1 [L]
RewriteRule ^search index.php?task=search [L]
RewriteRule ^submit index.php?task=submit [L]
RewriteRule ^r-([0-9]+)?-([0-9]+)? go.php?id=$1&ref=$2 [L]
RewriteRule ^r-([0-9]+)? go.php?id=$1 [L]
RewriteRule ^([^/\.]+)/([0-9]+)/([^/\.]+) index.php?task=view&id=$2&name=$3 [L]
RewriteRule ^news/([^/\.]+) index.php?task=news&name=$1 [L]
RewriteRule ^profile/([^/\.]+) index.php?task=profile&name=$1 [L]
RewriteRule ^news index.php?task=news [L]
RewriteRule ^page/([^/\.]+) index.php?task=view_page&name=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([0-9a-zA-Z'?-]+)/([0-9]+) index.php?task=category&name=$1&sortby=$2&page=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([^/\.]+) index.php?task=view&name=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+) index.php?task=category&name=$1 [L]
## www reslove ##
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
## www reslove ##
## index reslove ##
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ http://www.epicwallpaper.net/$1 [R=301,L]
## index reslove ##
Right below the RewriteEngine On line, add:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R] # <- for test, for prod use [L,R=301]
to enforce a no-trailing-slash policy.
To enforce a trailing-slash policy:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R] # <- for test, for prod use [L,R=301]
EDIT: commented the R=301 parts because, as explained in a comment:
Be careful with that R=301! Having it there makes many browsers cache the .htaccess-file indefinitely: It somehow becomes irreversible if you can't clear the browser-cache on all machines that opened it. When testing, better go with simple R or R=302
After you've completed your tests, you can use R=301.
To complement Jon Lin's answer, here is a no-trailing-slash technique that also works if the website is located in a directory (like example.org/blog/):
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
For the sake of completeness, here is an alternative emphasizing that REQUEST_URI starts with a slash (at least in .htaccess files):
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/$
RewriteRule ^ /%1 [R=301,L] <-- added slash here too, don't forget it
Just don't use %{REQUEST_URI} (.*)/$. Because in the root directory REQUEST_URI equals /, the leading slash, and it would be misinterpreted as a trailing slash.
If you are interested in more reading:
PR 3145 for Laravel
A discussion on commit 343c31e
(update: this technique is now implemented in Laravel 5.5)
This is what I've used for my latest app.
# redirect the main page to landing
##RedirectMatch 302 ^/$ /landing
# remove php ext from url
# https://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess
RewriteEngine on
# File exists but has a trailing slash
# https://stackoverflow.com/questions/21417263/htaccess-add-remove-trailing-slash-from-url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)/+$ /$1 [R=302,L,QSA]
# ok. It will still find the file but relative assets won't load
# e.g. page: /landing/ -> assets/js/main.js/main
# that's we have the rules above.
RewriteCond %{REQUEST_FILENAME} !\.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*?)/?$ $1.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## hide .html extension
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+).html
RewriteRule ^ %1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)/\s
RewriteRule ^ %1 [R=301,L]
## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^\.]+)$ $1.html [L]
<Files ~"^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</Files>
This removes html code or php if you supplement it. Allows you to add trailing slash and it come up as well as the url without the trailing slash all bypassing the 404 code. Plus a little added security.

Rewrite rule htaccess disturbing other rewrite rules

I have a page on my website that is getting generated dynamically to list all outlets based on cityf parameter and below is rewrite rule to convert it into SEO friendly URL and it is working pretty well.
RewriteRule ^([^/.]+)/?$ /cityres?cityf=$1 [L]
I have a blog page on my website and .htaccess is as below to convert SEO Friendly URL (http://example.com/title-of-blog)
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]
Now the problem here i am facing that when someone visits blog page then the link http://example.com/title-of-blog instead of displaying blog detail on the page, displays my Error message that No outlets near title-of-blog.
I got the issue that Apache is not able to identify when to rewrite cityres page and when to rewrite blogdetail page.
Someone suggested that Make sure that each rule has a common prefix (e.g. /blog/page1 and /news/page2). but i did not get that.
Any suggestions here please?
EDIT:
Whole htaccess is as below
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index
RewriteRule ^index\.php$ / [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
# remove .php from URL
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
# remove .html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)\.html$ /$1 [L,R=301]
ErrorDocument 404 /error-page
ErrorDocument 403 /error-page
RewriteRule ^food-([^-]*)-([^-]*)\.html$ /pdetail?res_id=$1&location=$2 [L]
RewriteRule ^foodies-([^-]*)-([^-]*)$ /pdetail_new?res_id=$1&location=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /pdetail_ne?location=$1&res_id=$2&name=$3 [L]
RewriteRule ^blog/([^/.]+)/?$ /blogdetail_fm?prmn=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !cityres
RewriteRule ^([^/.]+)/?$ /cityres?cityf=$1 [L]
Both your rules match the exact same pattern. Therefore, the first rule will always match and the second rule does nothing.
Looking at the first rule:
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]
This matches http://example.com/title-of-blog as well as http://example.com/city-name
When you look at it, you can tell which needs to be handled by blogdetail and which needs to be handled by cityres, but the regex ([^/.]+) sees them both as exactly the same, and matches both. Your regex doesn't know the difference, so whatever the first rule is, both URL's will get matched by it.
Like you said, someone suggested using a prefix. That way, the regex knows which is which:
RewriteRule ^city/([^/.]+)/?$ /cityres?cityf=$1 [L]
RewriteRule ^blog/([^/.]+)/?$ /blogdetail?prmn=$1 [L]
ANd your URLs will look like:
http://example.com/city/city-name
http://example.com/blog/title-of-blog
If you're really hung up about not adding prefixes, you can remove the second prefix:
RewriteRule ^city/([^/.]+)/?$ /cityres?cityf=$1 [L]
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]
So that you have:
http://example.com/city/city-name
http://example.com/title-of-blog
EDIT:
Your 500 server error is caused by the rules looping. You need to add a condition so that they won't keep matching:
RewriteRule ^blog/([^/.]+)/?$ /blogdetail?prmn=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !cityres
RewriteRule ^([^/.]+)/?$ /cityres?cityf=$1 [L]

Rewrite with htaccess

Here is the what I'm trying to do. I've got these urls, Old form and New form
site.com/video.php -> site.com/video
site.com/index.php?cat_id=1 -> site.com/some-cat-name
Here is my htaccess file which is working only if one of the last two is commented.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^\.htaccess$ - [F]
RewriteCond $1 !^(index\.php|img|js|css|admin|robots\.txt)
RewriteRule ^video$ /videos.php/$1 [L]
#RewriteRule ^(.*)$ /index.php/$1 [L]
if those two are active the server returns error 500
RewriteRule ^video$ /videos.php/$1 [L]
RewriteRule ^(.*)$ /index.php/$1 [L]
my logic is if first is matched video in the url redirect to videos.php and forget for all under this line, but seems to be in other way. Can some one to explain how to have some predifined urls and all others to go on index.php, tnx in advanced.
You are getting 500 (Internal server error) because your rule is looping in this rule:
RewriteRule ^(.*)$ /index.php/$1 [L]
To fix this rule have your .htaccess like this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|img|js|css|admin|robots\.txt)
RewriteRule ^video$ /videos.php/$1 [L]
# ignore existing files and directories from rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

.htaccess redirect woes — not completing

So, at the moment, my .htaccess looks a little like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{HTTP_HOST} ^colorspace\.am$ [OR]
RewriteCond %{HTTP_HOST} ^www\.colorspace\.am$
RewriteRule ^portfolio\/?(.*)$ "http\:\/\/i\.colorspace\.am\/portfolio$1" [R=301,L]
I want to move all the content from my root directory into a subdomain (which I've done) but I don't want the links people have to not work. www.colorspace.am/portfolio needs to redirect to i.colorspace.am/portfolio, and all the sets contained therein (ie /portfolio/YYYYMMDD) also need to be 'adjusted' on the fly (www.colorspace.am/portfolio/YYYYMMDD -> i.colorspace.am/porfolio/YYYYMMDD
NOTE: i.colorspace.am contain's 2011's content; ii.colorspace.am will contain 2012. They're two entirely different WP installs with their own respective databases. Not sure if it's relevant but..
What seems to be happening is that /portfolio is instructed to redirect to i.colorspace.am/portfolio, but for whatever reason it's ending up at i.colorspace.am
IF there's a way I can make any www.colorspace.am/folder/sub-folder redirect to i.colorspace.am/folder/sub-folder (wildcard?) ..that would be amazing. But I'd be just as happy with a single fully working redirect at this point.
NOTE: the redirect code was generated by my administration panel. I tried
Redirect /portfolio http://i.colorspace.am/portfolio
But it resulted in a 'too many redirects' error.
After the redirection from
RewriteRule ^portfolio\/?(.*)$ http//i.colorspace.am/portfolio$1 [R=301,L]
The rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Is accessed again. That is the reason it's ending up at i.colorspace.am.
Add these 2 rules
RewriteCond %{HTTP_HOST} ^(?:www\.)?i.colorspace\.am$
RewriteRule protfolio -[L]
in order mentioned below.
also change (just a small optimization)
RewriteCond %{HTTP_HOST} ^colorspace\.am$ [OR]
RewriteCond %{HTTP_HOST} ^www\.colorspace\.am$
RewriteRule ^portfolio\/?(.*)$ http//i.colorspace.am/portfolio$1 [R=301,L]
to
RewriteCond %{HTTP_HOST} ^(?:www\.)?colorspace\.am$
RewriteRule ^portfolio\/?(.*)$ http//i.colorspace.am/portfolio$1 [R=301,L]
and put it inside of IfModule block in the same order mentioned below.
Have just the below in your .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#add these 2 lines:
RewriteCond %{HTTP_HOST} ^(?:www\.)?i.colorspace\.am$
RewriteRule protfolio -[L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?colorspace\.am$
RewriteRule ^portfolio\/?(.*)$ http//i.colorspace.am/portfolio$1 [R=301,L]
</IfModule>
# END WordPress

Resources