I am looking to rewrite:
www.mysite.com/services/category1/?content=service1
to
www.mysite.com/services/category1/#tab-service1
I have this specific rewrite that I want to generalize for all the possible URLs on the site:
RewriteCond %{QUERY_STRING} content=service1
RewriteRule ^services/category1 services/category1/#tab-service1? [NE,R,L]
I tried the following but it didn't work :( -
RewriteCond %{QUERY_STRING} content=([a-zA-Z]+)
RewriteRule ^services/category1/?content=(.*)$ services/category1/#tab-$1 [NE,R,L]
Thanks.
You rule looks fine except for 1 thing
QUERYSTRING content=service1
isn't part of match in RewriteRule's pattern so you can't test query string (Url part after the ?) in pattern of a RewriteRule. To test url query string we use %{QUERY_STRING} variable like the one you are already using.
RewriteCond %{QUERY_STRING} content=([a-zA-Z]+)
RewriteRule ^services/category1/?$ services/category1/#tab-%1 [NE,R,L]
Related
I want to replace get parameter with mod_rewrite in .htaccess. I have Url www.domain.at/success?id=12345 and need to replace "id" with "vid" -> www.domain.at/success?vid=12345
This replacement must only work on "success" page/uri, but not on other pages of website.
I tried
RewriteEngine On
RewriteRule ^(.*)success?id=([^0-9]*)$ /$1success?vid=$2 [R=301,L]
But this is not working on dynamic part?
Thanks for help!
Martin
You have to match query parameters in RewriteCond separately from request URI like this:
RewriteCond %{QUERY_STRING} ^id=(.+)$ [NC]
RewriteRule ^success/?$ /$0?vid=%1 [R=301,L,NC]
I'm working with mod_rewrite under .htaccess, and I'm trying to redirect (R=301) an URL like this :
http://domain/index.php?folder=AB_CD
to an URL like this
http://domain/AB/CD/
How can I write the rule please ?
Try the following code in root/.htaccess :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^folder=([^_]+)_([^&]+)$ [NC]
RewriteRule ^index\.php$ http://domain.com/%1/%2/? [NC,L,R]
Explaination :
RewriteCond %{QUERY_STRING} ^folder=([^_]+)_([^&]+)$ [NC]
Checks to ensure that the url (index.php) has query strings with specific key and value, ( folder=foo_bar) acording to the regex pattern, if the url has valid query strings then the rule is processed
RewriteRule ^index\.php$ http://domain.com/%1/%2/? [NC,L,R]
index.php?query_strings gets redirected to /query/strings, if the condition is met.
Empty question mark ? at the end of the Rewrite target is important as it discards the orignal query strings, without it /index.php?folder=foo_bar redirects to /foo/bar/?folder=foo_bar appending the old query strings.
(Hope, this helps!)
What i'm trying to do is rewrite the following pattern
http://example.org/path?articleid=5657
to this pattern:
http://example.org/path?p=5657
Essentially it comes down to changing the key to the url parameter, i have searched extensively with no clear example of how to do this exact thing using only htaccess rewrite rules.
i've tried this general approach with no luck
RewriteCond %{QUERY_STRING} ^articleid=([0-9]*)$
RewriteRule ^articleid=([0-9]*)$ /?p=$1 [R=301,L]
You can't match against the query string in the pattern of a rule, you need to match against the URI:
RewriteCond %{QUERY_STRING} ^articleid=([0-9]*)$
RewriteRule ^path$ /path?p=$1 [R=301,L]
OK, say my blog site myurl.org as many links to a separate domain:
old.myurl.org?oldvar=foo
Only old.myurl.org no longer exists and has been replaced by new.myurl.org.
If the query string vars were the same on new.myurl.org, I believe I could rewrite from .htaccess using:
RewirteCond %{http_host} ^old.myurl.org$ [NC]
RewriteRule ^(.*)$ new.myurl.org [L,R=301,QSA]
The only problem is that I also need to change the query string var from oldvar to newvar and preserve it's data (foo).
There are plenty of examples of how to rewrite query string vars in different ways, but I can't seem to find an example of this scenario.
I need to rewrite:
old.myurl.org?oldvar=foo
To:
new.myurl.org?newvar=foo
Edit
Furthermore, I have several potential query string key values to account for, but not all will always be present.
So I may need:
old.myurl.org?oldvar=foo&oldvar2=bar --> new.myurl.org?newvar=foo&newvar2=bar
or
old.myurl.org?oldvar2=bar --> new.myurl.org?newvar2=bar
This may not even be possible, but in one case I need to strip off part of the query string value. So ?oldvar=foo{term} might need to become ?newvar=foo
Thanks again!
First condition match the domain, second condition match the query string and the rule will match your domain root:
RewriteCond %{HTTP_HOST} ^old\.myurl\.org$ [NC]
RewriteCond %{QUERY_STRING} ^oldvar=(\w+)$
RewriteRule ^$ http://new.myurl.org/?newvar=%1 [R=301,L]
You can place more than one query string as well:
RewriteCond %{HTTP_HOST} ^old\.myurl\.org$ [NC]
RewriteCond %{QUERY_STRING} ^oldvar=(\w+)$ [OR]
RewriteCond %{QUERY_STRING} ^oldvar=(\w+)&oldvar2=(\w+)$
RewriteRule ^$ http://new.myurl.org/?newvar=%1&newvar2=%2 [R=301,L]
RewriteCond %{HTTP_HOST} ^old\.myurl\.org$ [NC]
RewriteCond %{QUERY_STRING} ^oldvar2=(\w+)$
RewriteRule ^$ http://new.myurl.org/?newvar2=%1 [R=301,L]
To use a path like x/y/z as you have mentioned on the comment you change the RewriteRule for example the rule we are currently using, will only redirect from yourdomain.com/?... which is:
RewriteRule ^$ http://new.myurl.org/?newvar2=%1 [R=301,L]
If you want to catch a different path you would do this:
RewriteRule ^x/?$ http://new.myurl.org/?newvar2=%1 [R=301,L]
The above would catch yourdomain.com/x?... and yourdomain.com/x/?...
You can have more than one path as well and use a OR condition like this:
RewriteRule ^(x|x/y|x/y/z)/?$ http://new.myurl.org/?newvar2=%1 [R=301,L]
The above means we want to match x/?... OR x?... OR x/y?... and x/y/?... OR x/y/z?... OR x/y/z/?...
By encapsulating it on the parenthesis and using the | as separator and OR.
The ^ and $ means match from begin to end, so when it contains nothing means match nothing which means root folder domain.com/, when there is content it will match the content for instance domain.com/x or whatever you place into it.
i want an html redirection to a different php and params, the phps are inside a subdirectory , for example, from an /subdir/index.php?thread=23-post=12 /subdir/to showthread.php?topic=23&topic=12. But isn't working this:
RewriteRule ^/test/index\.php?thread=(.*)-post=(.*)$ /test/showthread.php?topic=$1&topic=$2 [R]
Any suggestion?
thanks in advance
You can't match against the query string in a RewriteRule. You need to match against the %{QUERY_STRING} var in a RewriteCond and use the % backreference:
RewriteCond %{QUERY_STRING} ^thread=([^-]+)-post=(.*)$
RewriteRule ^/?test/index\.php$ /test/showthread.php?topic=%1&topic=%2 [L,R]
Note that your rule maps 2 things to the topic query string param.