Remove query string - rewrite rule - string

How do i remove a query string from the end of a url?
To be more specific, this is my rewrite rule:
RewriteRule example-(.*).html$ examples/view-example.php?param1=parameter&param2=$1&split=-
and I want this to return a 404 or a redirect to www.mydomain.com/example-one.html :
www.mydomain.com/example-one.html?param1=parameter&param2=one&split=-
This is what i tried, it doesn't work:
RewriteCond %{REQUEST_URI} /examples/view-example\.php
RewriteCond %{QUERY_STRING} param1=parameter&param2=(.*)&split=-
RewriteRule ^(.*)$ http://mydomain.com/example-%1.html$
I think that RewriteRule ^(.*)$ http://mydomain.com/example-%1.html$ isn't correct..

This should do the trick:
RewriteCond %{QUERY_STRING} ^param1=parameter&param2=(.*)&split=-
RewriteRule ^/examples/view-example\.php$ http://mydomain.com/example-%1.html [R=301]
Though, I didn't understand what you wanted to do with www.mydomain.com/example-one.html?param1=parameter&param2=one&split=-

Related

rewrite url with query string issue

i have a issue with some rewriting url with htaccess.
i need the following:
old: mydomain.com/categorie/details.php?companyid=10
new: mydomain.com/item/?p=10
where i want to catch and replace the numbers (of several id) dynamically.
I've read the answer of the other question but that's only replace the query string. I quess i need some regex but i'm a newbee with that..
i have this in htaccess:
RewriteCond %{REQUEST_URI} ^/categorie/details\.php$
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$
RewriteRule ^(.*)$ www.mydomain.com/item/?p=%1/ [R=301,L]
Before i migrate i've tested this and it worked fine, for some reasons it won't work anymore after migration.
Do i miss something? Is this the wrong approach?
Kind regards,
Tom.
At this line
RewriteRule ^(.*)$ www.mydomain.com/item/?p=%1/ [R=301,L]
Put the ful URL like this :
RewriteRule ^(.*)$ http://www.example.com/item/?p=%1/ [R=301,L]
Also put this [NC] ,nocase to accept upper-case and lower-case, at the end of this line
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$ [NC].
So , your rules should look like this :
RewriteCond %{REQUEST_URI} ^/categorie/details\.php$
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/item/?p=%1 [R=301,L]
To sumerize all these rules more , do this :
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$ [NC]
RewriteRule ^categorie/details\.php$ http://www.example.com/item/?p=%1 [R=301,L]
And as you menthioned that it is same domain , let them be like this :
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$ [NC]
RewriteRule ^categorie/details\.php$ /item/?p=%1 [R=301,L]
No need to full URL.
Note: clear browser cache then test

Rewrite rule for ePages. From page to new URL

I try to make a rewrite rule for ePages.
Example:
Link is: www.test.com/epages/Test.sf?ObjectPath=/Shops/Test/Categories/testCat/testPage
New URL is: subtest.newtest.com
My Code is:
RewriteCond %{REQUEST_URI} ^/epages/Test.sf
RewriteCond %{QUERY_STRING} ^ObjectPath=/Shops/Test/Categories/testCat/testPage$
RewriteRule ^(.*)$ https://subtest.newtest.com/ [R=301]
But after the redirect I get:
https://subtest.newtest.com/?ObjectPath=/Shops/Test/Categories/testCat/testPage
But it need to be:
https://subtest.newtest.com/
What do I have to change?
Use with below,
RewriteRule ^(.*)$ https://subtest.newtest.com/? [R=301]

Mod Rewrite General Rule for ALL Pagination?

Could something like this be done? No matter what page or directory page=$1 appears under it'll be rewritten/redirected to /$1
For example:
file.php/1 would be file.php?page=1
dir/file/2 would be dir/file?page=2
dir/file.php?name=something/3 would be dir/file.php?name=something&page=3
Here's what I have so far:
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteCond %{QUERY_STRING} ^&page=([0-9-]+)/?$
RewriteRule ^(.+)\.php$ $1/%2 [R=301,L]
RewriteRule ^(.*)$ $1.php/$2
To cover the URI paths that end with /123 we can use this rule:
RewriteRule ^(.+)/([0-9]+)$ /$1?page=$2 [QSA,L,R]
So:
file.php/1 would be file.php?page=1
dir/file/2 would be dir/file?page=2
Note that this will also cover: dir/file/3?foo=bar would be dir/file?page=3&foo=bar
To cover the /123 that gets appended at the end of the actual query string, we can use this rule:
RewriteCond %{QUERY_STRING} (.*)/([0-9]+)$
RewriteRule ^(.+)$ /$1?%1&page=%2 [L]
So:
dir/file.php?name=something/3 would be dir/file.php?name=something&page=3

.htaccess mod_rewrite: rewriting querystring to path

How could I use a rewrite to change:
/?tag=foo
To:
/tag/foo
I tried:
RewriteCond %{QUERY_STRING} ^tag=(.+)$
RewriteRule ^(.*)$ http://www.example.com/tag/$1 [L]
But it did not work.
Try the following:
RewriteCond %{QUERY_STRING} ^tag=(.+)$
RewriteRule ^(.*)$ http://www.example.com/tag/%1 [L]
Usually, rewrites are used to achieve the opposite effect. Are you sure you don't really want to do the following?
RewriteRule ^tag/(.+)$ index.php?tag=$1 [L]
To avoid recursion, you should check the request line instead as the query string in %{QUERY_STRING} may already have been changed by another rule:
RewriteCond %{THE_REQUEST} ^GET\ /\?(([^&\s]*&)*)tag=([^&\s]+)&?([^\s]*)
RewriteRule ^(.*)$ /tag/%3?%1%4 [L,R=301]
Then you can rewrite that requests back internally without conflicts:
RewriteRule ^tag/(.*) index.php?tag=$1 [L]
I was trying to convert from a URL like this:
http://java.scandilabs.com/faq?key=Contents_of__gitigno
To a URL like this:
http://scandilabs.com/technology/knowledge/Contents_of__gitigno
Andrew's response above worked for me with the addition of a question mark at the end to discard the original query string:
RewriteCond %{HTTP_HOST} ^java
RewriteCond %{QUERY_STRING} ^key=(.+)$
RewriteRule ^(.*)$ http://scandilabs.com/technology/knowledge/%1? [R=301,L]
Note if you're on apache 2.4 or higher you can probably use the QSD flag instead of the question mark.

RewriteRule - a.php?a=123 to /b/123

I am trying to get Apache to redirect /a.php?a=123 to /b/123 (where 123 could be any number between 1 and 9999) but can't seem to get it to work.
This is what I have in htaccess:
RewriteEngine on
RewriteRule ^a.php?a=([0-9]+) /b/$1 [L]
RewriteRule ^a.php$ /c/ [L]
With this going to a.php?a=123 results in 404, but going to just a.php works as expected.
I tried escaping the ? (RewriteRule ^a.php\?a=([0-9]+) /b/$1 [L]) but it still doesn't work.
What am I doing wrong please?
The query string is not part of the URI path that is tested in the RewriteRule directive. This can only be tested with a RewriteCond directive:
RewriteCond %{QUERY_STRING} ^a=([0-9]+)$
RewriteRule ^a\.php$ /b/%1? [L,R]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^a\.php$ /c/ [L,R]
But if you want it the other way (requests of /b/123 are redirected to /a.php?a=123):
RewriteRule ^b/([0-9]+)$ a.php?a=$1 [L]

Resources