I tried the below .htaccess rewrite to internally rewrite the urls and make the overall urls SEF.
RewriteCond %{QUERY_STRING} ^searchword=(.*)&(.*)$ [NC]
RewriteRule ^searchword=(.*)&(.*)$ searchword=$1 [L]
My url is of the following form.
http://www.domain.com/?searchword=search+term&Search=&searchphrase=all&limit=50&ordering=newest&view=search&option=com_search
I want to make it of the form
http://www.domain.com/?searchword=search+term
You can use this rule:
RewriteCond %{QUERY_STRING} ^(searchword=[^&]+)$ [NC]
RewriteRule ^ /?%1&Search=&searchphrase=all&limit=50&ordering=newest&view=search&option=com_search [L,NE]
The best way to do this is by adding a router.php file to your component. Take a look at http://docs.joomla.org/Supporting_SEF_URLs_in_your_component
Related
I need to handle one query parameter specifically.
The url can look like the following:
https://example.com/?app=egov&map=1337
https://example.com/?app=egov&map=1337&showoptions=true
https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorld
The redirect should point to
https://example.com/contextName/resources/apps/egov/index.html?map=1337
https://example.com/contextName/resources/apps/egov/index.html?map=1337&showoptions=true
https://example.com/contextName/resources/apps/egov/index.html?map=1337&anotherparam=helloWorld
As you can see, the app-Parameter needs to be used as an URL-part; the others need to be used like traditional query-params.
I've figured out, how to implement the first part, where https://example.com/?app=egov points to https://example.com/contextName/resources/apps/egov/index.html using the following
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^app\=(.+)
RewriteRule ^/$ "/contextName/resources/apps/%1/index.html" [R,L,QSA]
but I'm struggeling how to realize the correct handling of the other params.
All these combination of query string can be handled by a single rule loke this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^app=([\w-]+)(?:&(.*))?$ [NC]
RewriteRule ^$ contextName/resources/apps/%1/index.html?%2 [L]
RewriteCond matches app query parameter and captures it in %1 while rest of the query string after & is captured in %2.
With your shown samples, please try following htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
####Apache documentation link https://httpd.apache.org/docs/trunk/rewrite/remapping.html for more info.
##Internal rewrite rule for URL https://example.com/?app=egov&map=1337
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2 [L]
##Internal rewrite rule for URL https://example.com/?app=egov&map=1337&showoptions=true
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=(.*)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2&showoptions=%3 [L]
##Internal rewrite rule for https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorl
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=([^&]*)&anotherparam=(.*)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2&anotherparam=%4 [L]
All of your URLs uri part is NULL, so rules are written based on that.
I'm trying to rewrite a few different URL's so they redirect to different places, however they aren't static URL's.
I need to redirect:
/index.php?app=core§ion=register
to for example:
htttp://www.mysite.com/register.php
However, what I want to do is just make sure that /index.php?app=core§ion=register starts with /index.php? but there may be more parameters included and also want to make sure it's still redirected if they are in a different position, so basically it just needs to match it if app=core and section=register exists in the URL.
Lastly, if I wanted to add a second one such as..
/index.php?app=core§ion=login to htttp://www.mysite.com/login.php how would I add a second one?
Try this rule:
RewriteCond %{QUERY_STRING} (?:^|&)app=core(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)section=(register|login)(?:&|$) [NC]
RewriteRule ^index\.php$ /%1.php? [NC,L,R=302]
As per comments:
RewriteCond %{QUERY_STRING} (?:^|&)app=core(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)section=(somepath)(?:&|$) [NC]
RewriteRule ^index\.php$ /mypath.php? [NC,L,R=302]
Our original urls began with a parameter and the following rewrite works to redirect them (thanks to Jon Lin).
However, the original parameter is being appended to redirect, so that
RewriteEngine On
RewriteCond %{QUERY_STRING} ^old-page$
RewriteRule ^$ /newpage [R=301,L]
ends up going to mydomain.com/newpage?old-page
Any idea how to fix? thanks again,
Geoff
Have it like this to strip off existing query string:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^old-page$ [NC]
RewriteRule ^$ /newpage? [R=301,L]
Take note of ? at the end of target URI, that is used to strip-off existing query string.
So basically, I am trying to remove a parameter from all URLs within my site.
For example:
http://www.mydomain.com.au/thispage.html?view=form
http://www.mydomain.com.au?view=form
http://www.mydomain.com.au/this-is-a-page.html?view=form
I require all the pages above to go to their version without this parameter, such as:
http://www.mydomain.com.au/thispage.html
http://www.mydomain.com.au
http://www.mydomain.com.au/this-is-a-page.html
The redirect must only occur when view=form, and not when view is equal to anything else.
Very new to regex and apache, so I am not to sure if this is even possible.
So far I have just been using the following code to point them to a 404, although this has been causing a few problems within webmaster tools:
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com\.au [NC]
RewriteCond %{QUERY_STRING} (^|&)view=
RewriteRule ^ - [L,R=404]
Use the following htaccess code
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)view=form(&|$) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI}? [R=301,L]
In case you want to preserve other query parameters (like ?foo1=bar1&view=form&foo2=bar2) use
RewriteCond %{QUERY_STRING} ^(?:(.*)&)?view=form(?:(&.*))?$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI}?$1$2 [R=301,L]
I'm trying to rewrite url from
example.com/test/?var=somevalue or example.com/test/page1.html?var=somevalue
to
example.com/test/somevalue/
Here is the code
RewriteCond %{REQUEST_URI} test/$
RewriteCond %{QUERY_STRING} var=([a-z]*|[a-z]*\-[a-z]*)$
RewriteRule ^test/(.*)$ /test/%1/page1.html? [R=301,L]
This for now only works with url example.com/test/?var=somevalue and what I can't get to work is url with page[1-9][0-9]*\.html before query_string
RewriteCond %{REQUEST_URI} test/(page[1-9][0-9]*\.html)?$
I think that's what you're looking for, piecing together the regex you already used with the one you described at the bottom of your query. But I'm a little shocked your rewrite rule worked at all. I would expect something more along the lines of:
RewriteRule ^test/([^/?])*\?([^&]+&)*var=([^&]+) /test/%3/page1.html
But I don't write that many RewriteRules, so I could be wrong.
So something like this?
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /test/([^\?]*)\?var=([^&\ ]+)([^\ ]*)
RewriteRule ^ /test/%3/%2?%4 [L,R=301]