.htaccess Rewrite URL with a query string - .htaccess

I have tried a ton of solutions posted at stackoverflow but nothing seems to work for me. I would like to rewrite a few URLs on my site to be search engine friendly.
I would like a URL like this:-
http://www.mysite.com/index.php?filter=accounts
To display as:-
http://www.mysite.com/accounts
A lot of posts around the web give this as the solution:-
RewriteRule ^([^/]+)/?$ index.php?filter=$1 [QSA,L]
But this doesn't do anything.
My site is a Joomla CMS site and there are already some rewrites within the .htaccess file. Could it be that I am putting my new RewriteRule is the wrong place?
Here is the full .htaccess file without my new RewriteRule added
Options +FollowSymLinks
RewriteEngine On
# prevents people from accessing anything with phpMyAdmin
RewriteRule phpMyAdmin - [F]
# force canonical www if request is for non-www or has port number etc
RewriteCond %{HTTP_HOST} !^(www\.mysite\.com)?$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} (/[^.]*|\.(php|html?|feed|pdf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
Any help would be much appreciated.
:-)

UPDATED
Now is tested. Try this:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-=_?]+)/?$ index.php?filter=$1 [L]
.htaccess goes in root directory.
The requested URL is http://www.mysite.com/accounts/ but could be anything: http://www.mysite.com/apples/.
NOTE: The URL displayed in the address bar is the one that was entered in that address bar. I assume is not http://www.mysite.com/index.php?filter=accounts because I guess the purpose of the redirection is to replace it with a more friendly one (http://www.mysite.com/accounts), which is the one that has to be typed in the address bar, not the other way around. Little confusing but I hope it makes sense.
To test it, include the following only code at index.php in root directory.
<?php
if ($_GET['filter'] == 'accounts') {
echo "This is Accounts<br /><br />";
}else {
echo "This is INDEX<br /><br />";
}
?>
As to where to place the rewrite rules inside .htaccess, I could not know. I guess you have to try it before and after the actual rules.

Related

RewriteEngine Adds relative path

I have such rewrite rules
RewriteEngine On
RewriteCond %{HTTP_HOST} ^monkey.pl(.*) [NC]
RewriteRule ^(.*)$ http://www.monkey.pl/$1 [R=301,L]
RewriteRule ^horse.html$ /dog.html
and when I go to the monkey.pl/horse.html I get the message:
The requested URL /home/login/monkey/dog.html was not found on this server.
How can I get this to work. Basically what I'm trying to do is to change address of urls like:
http://www.monkey.pl/produkty.php?strona=1
to be displayed as
http://www.monkey.pl/produkty/czesci_do_mixokretow.html
but none of my rules are working. Therefore I'm trying to come with solution.
I tried many varations and I couldn't get it to work. I don't want to rewrite whole page. Just 6 pages which I need to change url and that's all. Fixed translation url => url.
If you are only doing a handful of URLs then you can do them this way.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} /+produkty\.php\?strona=1 [NC]
RewriteRule ^ /produkty/czesci_do_mixo‌​kretow\.html [R=302,L]
RewriteRule ^produkty/czesci_do_mixo‌​kretow\.html$ /produkty.php?strona=1 [L]
RewriteRule ^horse\.html$ /dog.html [L]

URL Rewriting with "!" (exclamation mark)

I am stuck with this challenge, and I really hope that someone can help me out.
I am currently working on an API, and a friendly URL is needed to call on the API.
A VOLKSWAGEN manufacture has released a car called UP!
If you notice there is an exclamation on the brand type. This has created an issue with the .htaccess because it lands to 404 page.
Sample URL:
http://mysite.com/cars/new/VOLKSWAGEN/UP!
Has anyone ever encountered this issue, and solved it?
This is my .htaccess file
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
Many thanks.
Insert this rule just below RewriteEngine On line:
RewriteRule ^(.*?)!$ /$1 [R=301,L]
This will remove trailing ! from all URIs.
You need to be using URL encoding. I'm not quite sure what you mean by "friendly", but you simply can't have an exclamation-point character in an HTTP URL. Most browsers these days are translating encoded characters into their equivalents on the address bar, though.

Trouble with mod_rewrite, subdomain and codeigniter

I have a codeigniter installation at example.com/ci.
I have a subdomain foo.example.com. The document root for the foo subdomain is set to be home/public_html/ci.
I'm using the following rule in .htaccess to send requests for foo.example.com to example.com/ci/city/foo.
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule (.*) http://example.com/ci/city/%1/$1 [L]
It all works like I want it to except that the address bar url changes from foo.example.com to example.com/ci/city/foo. I would like it to remain foo.example.com. There is no R=301 in the RewriteRule (used to be but I removed it). The .htaccess file is in the ci/ folder and the rule is above all the codeigniter stuff.
The redirect works perfectly and the url remains foo.example.com with (Jon Lin's answer)
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteCond %{REQUEST_URI} !/city/
RewriteRule (.*) /city/%1/$1 [L]
but the codeigniter default controller is called instead of the foo method in the city controller.
Any help is appreciated.
When your rewrite rule's target has an http://example.com in it, a 302 redirect is implicit regardless of whether an R flag is used or not. You need to provide the URI path based on the subdomain's document root, so I'm assuming you want something like:
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteCond %{REQUEST_URI} !/city/
RewriteRule (.*) /city/%1/$1 [L]
If the subdomain's document root is in the /ci/ directory.
The other option is to use the P flag to reverse proxy the request:
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule (.*) http://example.com/ci/city/%1/$1 [L,P]
Your mileage may vary with this (might need to finesse it to fit your server and conditions), but doing some testing on my Mac, here's what I had mild success with:
Directory Structure
public_html/
ci/
application/
system/
.htaccess
index.php
I'm assuming that you have other stuff in your root public_html directory. So I'm letting the .htaccess focus on the CodeIgniter-related stuff by leaving it in the ci dir.
.htaccess
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.ciwildsub\.dev [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/city/%1/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
It's fairly self explanatory, but the first block is your subdomain check. I didn't bother excluding www but you may want to (as I said, your mileage may vary). The second block is a standard CodeIgniter index.php removal.
These rules will only apply to sub.example.com or example.com/ci/ URLs, since as I said, I assume your root has stuff that shouldn't be disturbed by rewrites.
CodeIgniter Config
$config['uri_protocol'] = 'PATH_INFO';
Because of the way Apache handles a URL like example.com/index.php/controller/method, it bypasses the index.php and handles it like any other directory segment. Also, mod_rewrite doesn't necessarily stop at the [L] tag -- it stops processing the .htaccess at that point, passes through the RewriteRule, and then runs that URL through the .htaccess. Setting PATH_INFO helps make sure CodeIgniter pulls the current URI correctly, and our .htaccess doesn't get stuck in a validation loop.
I will note, though, that I'm not entirely happy with what I see in my RewriteLog output -- there has to be a way to optimize this further, I'm just not sure of it yet (I'm done tinkering with this for today!). Sorry if any of the explanation here is a little out of whack - I'm not a server admin or mod_rewrite expert, I've just had fun tinkering with this. If I manage to find a better solution, I'll be sure to update this.
Looks like the END flag would be perfect for situations like this (to prevent [L] loops), but it's only available in Apache 2.3.9+. The search continues.
I got it to work correctly using the following rewrite rule
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteCond %{REQUEST_URI} !/city/
RewriteRule (.*) /city/%1/$1 [L]
and by setting
$config['uri_protocol'] = 'ORIG_PATH_INFO';
in the codeigniter config file. Thanks for all the help.
This worked for me
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /file_path/to/subdomain
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>

301 redirect only if URI doesn't contain specific string

I have a drupal website which has been split up to two separate websites, and now I need to setup some rewrite rules in order to drive traffic to the new site.
Original website looks like this:
http://www.website.com (frontpage)
http://www.website.com/web1/subpage1 (subpage)
http://www.website.com/web1/subpage2 (subpage)
http://www.website.com/subpage3 (subpage)
http://www.website.com/subpage4 (subpage)
All references to subpages that are not in the web1-category have been removed from the website, but the pages are still published and they still show up in Google.
What I need is a rewrite rule that redirects from "website.com" to the frontpage of "new-website.com" if the user tries to access a page that is not the frontpage and not in the web1-category.
I suppose a rewrite-rule checking for the string "web1" in the URI would be the answer to my problem, but unfortunately I have no idea how to write the syntax.
Any help would be appreciated.
Thanks in advance.
EDIT:
My htaccess file with #zessx proposed solution:
Options -Indexes
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^my-website\.com$ [NC]
RewriteRule ^(.*)$ http://www.my-website.com/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !web1
RewriteRule ^(.+)$ http://www.my-new-website.com [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
This is what you need :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !web1
RewriteRule ^(.+)$ http://new-website.com [L,R=301]
A colleague of mine, found a solution:
RewriteCond %{REQUEST_URI} !^/web1(/|$)
RewriteCond %{REQUEST_URI} !^/admin(/|$)
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^([\w/]*)$ http://www.new-website.com [L]`

How to change URL? htaccess

What I have
newsy/czytaj/items/odbierz-250zl-na-reklame.html
This is what I would have
newsy/odbierz-250zl-na-reklame.html
How to do this with mod-rewrite? I don't understand RewriteRule.
My .htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} (ftp|https?):|/etc/ [NC,OR]
RewriteCond %{QUERY_STRING} (ftp|https?):|/etc/ [NC]
RewriteRule .* - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*\.html$ index.php [L]
RewriteEngine on
RewriteRule ^newsy/([^\./]+)\.html /newsy/czytaj/items/$1.html [L]
This will rewrite anything that starts with newsy and add a /czytaj/items between it and the html file.
In principle you just create a corresponding rewrite rule:
RewriteRule ^newsy/czytaj/items/(.+) /newsy/$1 [L]
It is crucial not to omit [L]flag. Otherwise your rewrite engine may get stuck in an endless loop. Also in the beginning of the .htaccessfile remember to enable mod_rewrite with:
RewriteEngine On
For more help on mod_rewrite, I recommend checking out mod_rewrite-cheatsheet. For an exhaustive URl Rewriting Guide see a corresponding page from Apache 2.0 Documentation.

Resources