mod_rewrite: Convert query strings to hyphenated page links - string

How do I convert this URL to something that looks like a page:
http://www.example.com/search-results/?action=search&type%5Bequal%5D=Blog&keywords%5Bany_words%5D=visual+basic
I want to convert the above URL to:
http://www.example.com/blogs/visual-basic
When the user clicks on the converted short link 2, it should go to the long URL 1 listed on the top.
I tried this but it does not work:
RewriteRule ^blogs/([a-zA-Z0-9_-]+)\.html$ /search-results/?action=search&type%5Bequal%5D=Blog&keywords%5Bany_words%5D=$1 [NC,L]

Either you question is wrong or your RewriteRule is wrong.
You want the user to type http://www.example.com/blogs/visual-basic
and this address to be internally rewritten to:
http://www.example.com/search-results/?action=search&type%5Bequal%5D=Blog&keywords%5Bany_words%5D=visual+basic
There's no "html" ending the URL in your user address. Thus, try this, it should work:
RewriteRule ^blogs/([a-zA-Z0-9_-]+)$ /search-results/?action=search&type%5Bequal%5D=Blog&keywords%5Bany_words%5D=$1 [NC,L]

Related

Use htaccess to change query parameter to iOS app-specific deep-link [duplicate]

I am trying to do the following:
User visits URL with query parameter: http://www.example.com/?invite=1234
I then want them to be deep linked into the app on their iOS device, so they go to: app_name://1234
Any suggestions on how to accomplish this in my .htaccess file?
I tried this but it doesn't work:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^invite/(.*)/$ app_name://$1 [NC,L]
If RewriteRule won't work, can anyone send me an example code for RewriteCond or JavaScript to achieve what I need?
Not sure how this will work with the iOS device, but anyway...
RewriteRule ^invite/(.*)/$ app_name://$1 [NC,L]
This doesn't match the given URL. This would match a requested URL of the form example.com/invite/1234/. However, you are also matching anything - your example URL contains digits only.
The RewriteRule pattern matches against the URL-path only, you need to use a RewriteCond directive in order to match the query string. So, to match example.com/?invite=1234 (which has an empty URL-path), you would need to do something like the following instead:
RewriteCond %{QUERY_STRING} ^invite=([^&]+)
RewriteRule ^$ app_name://%1 [R,L]
The %1 backreference refers back to the last matched CondPattern.
I've also restricted the invite parameter value to at least 1 character - or do you really want to allow empty parameter values through? If the value can be only digits then you should limit the pattern to only digits. eg. ^invite=(\d+).
I've include the R flag - since this would have to be an external redirect - if it's going to work at all.
However, this may not work at all unless Apache is aware of the app_name protocol. If its not then it will simply be seen as a relative URL and result in a malformed redirect.

Link Redirect with htaccess

I want to redirect all type of links like that :
www.sitename.com/link1-XXX.html
to
www.sitename.com/link1-XXX/
I don't want to redirect all .html links. I want to redirect all html links which end with XXX.
Solution :
RewriteRule ^(.*)XXX\.html$ /$XXX/ [R=301,L]
You did not specified the question, just told us what you want to do, not what is your problem...
if your problem is, that you dont know how to redirect only links like you described, then put this in your .htaccess with RewriteEngine on:
RewriteRule ^/(.*)-XXX\.html?$ /$1-XXX/ [L,QSA]
I did not really tested this regex..
It should take anything that end with /"something"-XXX.htm and /"something"-XXX.html and redirect it to subaddr named as "something"-XXX/
if you would need append some GET params to the processed url, you should change the regex so it ends like this:
...html?(.*)$
meaning, there can be anything after htm or html

have a unique number in the url - do not match

This is how I would like to get my url to look at it any other way than before.
The problem is such that I must have forum and after forum content unique number that is in the database and after the url of the content.
I could imagine that it looked like this:
/forum/1/hello-world-stackoverflow-danmark/
now on my .htaccess file:
RewriteRule ^forum/([^/.]*)/([^/.]*)/?$ /forum-s.php?id=$1&url=$2 [L]
and now it:
www.hello-world.com/forum/velkommen-til-traenigsmakker---sjaelland/
I will have its to
www.hello-world.com/forum/1/velkommen-til-traenigsmakker---sjaelland/
Be sure to have RewriteEngine On
The code you have should work, you can optimize it like this to ensure the numbers.
RewriteRule ^forum/([0-9]+)/([^/.]*)/?$ /forum-s.php?id=$1&url=$2 [L]

301 htaccess redirect dynamic url help needed

I'm trying to redirect this
hhttp://www.website.net/forum/index.php?showtopic=12345
to
hhttp://www.website.ORG/forum/t12345
12345 being the dynamic topic ID
I also need any information to be stripped away if it is found after the topic ID, for example
hhttp://www.website.net/forum/index.php?showtopic=12345&view=getlastpost
I want &view=getlastpost or any similar that may appear after the ID number to be get rid of.
I've tried
RewriteCond %{QUERY_STRING} ^(([^&]&))showtopic=([^&]+)&?(.*)?$
RewriteRule ^index.php$ http://www.website.org/forum/t%3?%1%4/ [L,R=301]
but it didn't work. I get trash in the URL.
hhttp://www.website.org/forum/index.php?showtopic=29294&view=getlastpost (when that link is clicked - the result is hhttp://www.website.net/forum/t29294?view=getlastpost/)
hhttp://www.website.org/forum/index.php?showtopic=29029 (when that link is clicked - the result is hhttp://www.website.net/forum/t29029?/).
How can I clear it out?
$2 implies there are two bracketed areas, but I only see one in your rule, so changed that to $1.
Also your URL starts /forum/ so need to include that in the rule.
And the . in index.php needs to be escaped if you don't want it treated as a regex special character.
And if you want to ditch anything after the showtopic=1234 then just remove the $ that indicates the end of the string
RewriteRule ^forum/index\.php?showtopic=([0-9]*) http://www.website.org/forum/t$1/ [L,R=301]

URL Beautification using .htaccess

in search of a more userfriendly URL, how do i achieve both of the following, elegantly using only .htaccess?
/de/somepage
going to /somepage?ln=de
/zh-CN/somepage#7
going to /somepage?ln=zh-CN#7
summary:
/[language]/[pagefilenameWithoutExtension][optional anchor#][a number from 0-9]
should load (without changing url)
/[pagefilenameWithoutExtension]?ln=[language][optional anchor#][a number from 0-9]
UPDATE, after provided solution:
1. exception /zh-CN/somepage should be reachable as /cn/somepage
2. php generated thumbnails now dont load anymore like:
img src="imgcpu?src=someimage.jpg&w=25&h=25&c=f&f=bw"
RewriteRule ^([a-z][a-z](-[A-Z][A-Z])?)/(.*) /$3?ln=$1 [L]
You don't need to do anything for fragments (eg: #7). They aren't sent to the server. They're handled entirely by the browser.
Update:
If you really want to treat zh-CN as a special case, you could do something like:
RewriteRule ^zh-CN/(.*) /$1?ln=zh-CN [L]
RewriteRule ^cn/(.*) /$1?ln=zh-CN [L]
RewriteRule ^([a-z][a-z])/(.*) /$2?ln=$1 [L]
I would suggest the following -
RewriteEngine on
RewriteRule ^([a-z][a-z])/([a-zA-Z]+) /$2?ln=$1
RewriteRule ^([a-z][a-z])/([a-zA-Z]+#([0-9])+) /$2?ln=$1$3
The first rule takes care of URLs like /de/somepage. The language should be of exactly two characters
length and must contain only a to z characters.
The second rule takes care of URLs like /uk/somepage#7.

Resources