mod_rewrite advice - .htaccess

I was wondering if you could advise if there is a more efficient way of writing what I have below? Could this be achieved in the 1 rule?
RewriteRule ^search/(cars|boats|plant)/([-a-zA-Z0-9]+)/type-([0-9]+)/price-([0-9]+)-([0-9]+)/?$ search.php?category=$1&location=$2&type=$3&minprice=$4&maxprice=$5 [L]
RewriteRule ^search/(cars|boats|plant)/([-a-zA-Z0-9]+)/type-([0-9]+)/?$ search.php?category=$1&location=$2&type=$3 [L]
RewriteRule ^search/(cars|boats|plant)/([-a-zA-Z0-9]+)/?$ search.php?category=$1&location=$2 [L]
RewriteRule ^search/(cars|boats|plant)/?$ search.php?category=$1 [L]
Also, I am having difficulty obtaining a friendly URL after a form is submitted. How to do this?

Perhaps you'd be better off just writing one rule to redirect from /search to search.php, and then parsing the rest of the URI there? (I.e. redirect to search.php?q=REST_OF_URI_GOES_HERE)

Related

How to write multiple pages with same parameters in .htaccess file

I am having 2 pages (page1.php , page2.php) taking 2 parameters along with them in the URL.
RewriteRule ^([^/\.]+)/([^/\.]+)?$ page1.php?page=$1&detail=$2
RewriteRule ^([^/\.]+)/([^/\.]+)?$ page2.php?type=$1&val=$2
The above code is creating error while redirecting. Can anybody tell what is the right Rule ?
Try This,
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /page1.php?page=$1&detail=$2
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /page2.php?type=$1&val=$2
I think you were missing just a few /

How to rewrite a long URL with numerical value to short url with same value

Please Help with non-stardart Long URL
http://example.com/media/player/player.swf?f=http://example.com/media/player/config.php?vkey=12345
Want rewrite to short URL
http://example.com/embed/12345
I assume you mean it the other way around, if someone request http://example.com/embed/12345 you want this internal rewritten to http://example.com/media/player/player.swf?f=http://example.com/media/player/config.php?vkey=12345
If so this works as follows:
RewriteEngine On
RewriteRule ^/?embed/([^/]+)$ /media/player/player.swf?f=http://example.com/media/player/config.php?vkey=$1 [L]
If you really want it the other way around than you have to do it like so:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^f=http://example\.com/media/player/config\.php\?vkey=([^&]+)$
RewriteRule ^/?media/player/player\.swf$ /embed/%1 [L]
If you want a redirect than change [L] to [R=301,L]

mod_rewrite for SEO friendly URL using htaccess

I am trying to write SEO friendly URL of below URL.
www.url.com/item.php?id=_84_113_112&lang=en
I have written below code:
RewriteRule ^(.*)/(.*)/$ item.php?id=$2&lang=$1 [QSA,L]
But not working, can anyone help?
Probably extra / at the end (before $) is the problem.
Change it to like this.
RewriteRule ^(.*)/(.*)$ item.php?id=$2&lang=$1 [QSA,L]
You can use this -
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ item.php?id=$1&lang=$2

RewriteRule does not work, while the rest do

My blog's .htaccess is setup in such a way that one page is accessed through multiple URLs, and displays different content depending on which URL is visited.
http://kn3rdmeister.com/category/blog/
http://kn3rdmeister.com/2012/
http://kn3rdmeister.com/2012/07/
all are actually using http://kn3rdmeister.com/blog.php.
The .htaccess file is very handy in the sense that I only need to redirect to one page (pretty much ever) just with different query strings. After a lot messing around with 'em, all of my rules finally work, and I'm dang glad that they do. Well, almost all of them work. The last one does not.
the .htaccess:
RewriteEngine On
RewriteRule ^blog\.php$ /category/blog/ [R=301,L]
RewriteRule ^category/blog/?$ blog.php [L]
RewriteRule ^category/blog/page/?$ /category/blog/ [R=301,L]
RewriteRule ^category/blog/page/([0-9]*)/?$ /category/blog/?pagenum=$1 [L]
RewriteRule ^([0-9]{4})/?$ /category/blog/?year=$1 [L]
RewriteRule ^([0-9]{4})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2 [L]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2&day=$3 [L]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$ /category/blog/?url=http://kn3rdmeister.com/$1/$2/$3/$4/ [L]
The last rule is supposed to redirect to the "permanent link" page for each blog post. Being that each URL is unique, I'm using the post URLs as the unique identifier. Essentially, it is supposed to pass the "url" query string through "blog.php". The PHP script takes over, sees that the "url" query string is set, and then loads the only post with that exact URL in it's row.
The script works, but the redirect doesn't. Going directly to
http://kn3rdmeister.com/blog.php?url=http://kn3rdmeister.com/2012/07/04/amsterdam-ave/
will load the right content. However, going to
http://kn3rdmeister.com/2012/07/04/amsterdam-ave/
doesn't.
Try adding QSA (Query String Append). Also, invert rules so that "deeper" links go on top.
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$ /category/blog/?url=http://kn3rdmeister.com/$1/$2/$3/$4/ [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2&day=$3 [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2 [QSA,L]
RewriteRule ^([0-9]{4})/?$ /category/blog/?year=$1 [QSA,L]
But, you can't use rewritten links in other rules. So wherever you have category/blog/ replace it with blog.php.
Whilst webarto comments are good advice, your problem is a missing [:
^([0-9]{4})/([0-9]{2})/([0-9]{2})/([^/]+)/?$
not
^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$

Multilanguage site, subdirectories as language (RewriteRule)

For my website i want to rewrite my url to:
www.xxx.com/en/index.php instead of www.xxx.com/index.php?language=en
www.xxx.com/nl/index.php instead of www.xxx.com/index.php?language=nl
www.xxx.com should be www.xxx.com/en/
This actually works i have added these rewrite rules
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www\.xxx\.com(\/?)$
RewriteRule (.*) http://www.xxx.com/en/$1 [R=302,L]
RewriteRule ^(nl|en)/(.*)$ $2?language=$1&%{QUERY_STRING} [L]
The R=302 is added for testing purposes
Are the above given RewriteCond and RewriteRule good enough or are there other, maybe shorter or better ways to rewrite .com/ to .com/en/
If the url is edited to a language that does not exists i want this user to be redirect to english.
Example
www.xxx.com/es/index.php is requested, this language does not exists or no language is given i want the user to be redirect to www.xxx.com/en/index.php
I tried the following:
RewriteRule ^([^n][^l]|[^e][^n])/(.*)$ en/$2%{QUERY_STRING} [L,R=302]
This works in some cases but if www.xxx.com/ne/index.php is entered it is considered as valid and not rewritten. www.xxx.com/index.php is also not rewritten to www.xxx.com/en/index.php
Could someone help me to fix this?
Thanks in advance!
Try this rule:
RewriteCond $1 !^(en|nl)$
RewriteRule ^([a-z]{2})/(.*)$ en/$2 [L,R=302]

Resources