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.
Related
I had a previous post regarding this matter:
ISAPI_Rewrite and Coldfusion Rules
With the help of Helicon Support we were able to create the following rules:
RewriteEngine on
RewriteRule on
RewriteBase /
RewriteCond %{QUERY_STRING} ^filter=brand&brand_id=([^&]+)&brand_name=([^&]+)$ [NC]
RewriteRule ^products-search\.cfm$ /search/%1/%2.cfm? [NC,R=301,L]
RewriteCond %{QUERY_STRING} ^$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/([^/]+)/([^._]+)\.cfm$ /products-search.cfm?filter=brand&brand_id=$1&brand_name=$2 [NC,L]
RewriteCond %{QUERY_STRING} ^product_id=([^&]+)&product_title=([^&]+)$
RewriteRule ^product-details\.cfm$ /%1/%2.cfm? [NC,R=301,L]
RewriteCond %{QUERY_STRING} ^$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^._]+)\.cfm$ /product-details.cfm?product_id=$1&product_title=$2 [NC,L]
Their help was much appreciated as i am very new to URL Rewriting. The issue i am having now is that the first rule works, it turns the url from:
www.mysite.com/products-search.cfm?filter=brand&brand_id=98&brand_name=HAMANN
to
www.mysite.com/search/98/HAMANN.cfm
Since i am new to this, i do not understand how to get the css/js/images folders to display on this page, what rule or condition do i use for the RewriteBase / part so that everything on the page loads correctly?
The second part of the .htaccess rules do not actually work which change:
product-details.cfm?product_id=1&product_title=This Is A New Product
to
products/This_Is_A_New_Product.cfm
Any help would be greatly appreciated
I am trying to 301-redirect example.com to www.example.com by .htaccess:
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Problem: my-site.com/contact.html redirects to www.example.com/index.php instead of www.example.com/contact.html.
How can I accomplish that?
UPDATE: this is the complete .htaccess for my Joomla-site (comments stripped):
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]
###### this is the code in question...
RewriteCond %{HTTP_HOST} ^example$ [NC]
RewriteRule ^(.*)$ http://www.example/$1 [R=301,L]
# RewriteBase /
## Begin - Joomla! core SEF Section.
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]
# take care of sitemap.xml
RewriteCond %{REQUEST_URI} ^/sitemap.xml
RewriteRule .* /index.php?option=com_xmap&view=xml&tmpl=component&id=1&format=html [L]
I moved my two lines up just below RewriteEnigine On - but had the same problem again. Clueless.
Problem: example.com/contact.html redirects to www.example.com/index.php instead of www.example.com/contact.html.
I'm going to guess that you have other rules in your htaccess file, one that routes everything to index.php. You need to make sure that your redirect rule is before any other rule that you have.
After checking your htaccess file, you need to add an L flag at the end of your first rule:
RewriteRule .* index.php [F,L]
Because without it, the redirect gets applied even if the first rule gets applied (e.g. it gets rewritten to index.php with a 403 response waiting in the queue).
Firstly uncomment line
# RewriteBase / //Remove #
Below the above line write this code
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I guess I'm already in the wrong for asking for clarification but you can just delete this.
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
I see you added an exclamation point and I've seen this redirect elsewhere without it. What does the exclamation point do in this case or is that incorrect?
Have a great day.
I'm having an issue with a query string, although I'm not sure what i'm tryign to do is possible.
I have the following url with a search query attached:
subdomain.mysite.com/search/?search=searchquery
but I need it to redirect to the following url including the query string:
subdomain.mysite.com/search/?rs=searchquery
I was wondering if this is possible with a mod_rewrite?
I've tried the following:
RewriteCond %{REQUEST_URI} ^/search/?$
RewriteCond %{QUERY_STRING} ^search=([0-9]*)$
RewriteRule ^(.*)$ http://subdomain.mysite.com/search/?rs=% [L,R=301]
I don't know if my Syntax is incorrect or i'm just barking up the wrong tree. Any help would be awesome.
This is the Start of my HTAccess File - This is a Wordpress site with the rewrite rule provided by user: Amine Hajyoussef:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_URI} ^/search/?$
RewriteCond %{QUERY_STRING} ^search=([^&]*)$
RewriteRule ^(.*)$ /search/?rs=%1 [L,R=301]
</IfModule>
Unfortunately this doesnt appear to work? Any ideas?
you forgot the 1 after %, i have also changed the search string so it can be anything instead of just number.
RewriteCond %{REQUEST_URI} ^/search/?$
RewriteCond %{QUERY_STRING} ^search=([^&]*)$
RewriteRule ^(.*)$ /search/?rs=%1 [L,R=301]
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.
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.