.htaccess rewrite rule with multiple {QUERY_STRING}s - regex

I am working on a website project where we basically move from TYPO3 to a WordPress & Magento-solution.
Before launching the new site, I would like to add rewrite rules to point the old (TYPO3, non-SEF) URLs to the corresponding new ones. I have an Excel-list with around 1000 URLs that I somehow would like to add to htaccess and create 301's.
If you have a better approach for this, I'd be thankful.
What I am struggling with is:
The "old" URL structure looks something like ?id=123\&user_e15proddb1_pi1[domain]=42
the correcponding new URL would be
/de/alle-produkte/neuheiten.html
RewriteEngine is on, RewriteBase is /.
I tried
RewriteCond %{QUERY_STRING} ^id=123\&user_e15proddb1_pi1[domain]=42$
RewriteRule . /de/alle-produkte/neuheiten.html [R=301,L]
With additionally escaping the _and the [] with no avail.
I tried to seperate the {QUERY_STRING}s into two by
RewriteCond %{QUERY_STRING} ^id=123$
RewriteCond %{QUERY_STRING} ^user_e15proddb1_pi1[domain]=42$
followed by TheRule. Also no avail.
Rewriting itself works, because I tried
RewriteRule .id=123\&user_e15proddb1_pi1\[domain\]=42$ /de/alle-produkte/neuheiten.html [R=301,L]
But that only works without the question mark in the beginning.
Could you give me a hint on what I am doing wrong?

You can use this rule by escaping [ and ]:
RewriteCond %{QUERY_STRING} ^id=123&user_e15proddb1_pi1\[domain\]=42$
RewriteRule ^ /de/alle-produkte/neuheiten.html? [R=302,L]
Also note ? at the end of target URI to strip off any existing query string to prevent a redirect loop.

I got a solution to and made this one work:
RewriteCond %{QUERY_STRING} ^id=123\&user\_e15proddb1\_pi1\[domain\]=42$
RewriteRule (.*) /de/alle-produkte/neuheiten.html? [R=301,L]
Proably something with escaping those characters was going wrong when I tried over and over again.

Related

RegEx matching for URL redirect in htaccess

I've some dynamic urls. If I took a url with a query parameter, it leads to 404 page. So I would like to do a redirection using htaccess. I tried with a matching url pattern regex and it is not redirecting.
Url structure will be /detail/2019-12/news/news-title-12-2019.html?something and which I need to redirect to /detail/2019-12/news/news-title-12-2019.html
I tried something like this, but it is not redirecting;
RewriteCond %{QUERY_STRING} .
RewriteRule ^detail/\d{4}-\d{2}/news/(?=\S*['-])([a-zA-Z0-9'-]+\.html)\?\S*$ %{REQUEST_URI}? [NC,L,R=301]
How do I solve this problem?
This RegEx might help you to write your RewriteRul:
^(.+\.html)(\?.+)$
I'm not quite sure, but your code might look like:
RewriteCond %{QUERY_STRING} .
RewriteRule ^(.+\.html)(\?.+)$ http://domain_goes_here/$1 [NC,L,R=301]
RewriteCond %{QUERY_STRING} .
RewriteRule ^(.+\.html)(\?.+)$ http://domain_goes_here/$1 [NC,L,R=302]
You also might need to:
restart apache
delete your browser cache
If you wish to add boundaries to your expression, this RegEx might help you to do so:
^(\/detail\/[0-9-]+\/news\/[a-z0-9-]+\.html)(\?.+)$
Code:
RewriteCond %{QUERY_STRING} .
RewriteRule ^(\/detail\/[0-9-]+\/news\/[a-z0-9-]+\.html)(\?.+)$ http://domain_goes_here/$1 [NC,L,R=301]
RewriteCond %{QUERY_STRING} .
RewriteRule ^(\/detail\/[0-9-]+\/news\/[a-z0-9-]+\.html)(\?.+)$ http://domain_goes_here/$1 [NC,L,R=302]
The reason why you rule isn't working is because you are checking Querystring ? in your rule regex. The pattern of RewriteRule is for URL path only .
The following should work for you
RewriteCond %{QUERY_STRING} .
RewriteRule ^detail/\d{4}-\d{2}/news/ %{RRQUEST_URI}? [L,R=301]
Make sure to clear your browser cache or use a different browser for testing this.

Apache url rewriting and loop

setting url rewriting to have nice urls, i have existing urls like that :
/xxx/test.php
but in the background, it is allways going to the same script with a query :
/xxx/index.php?id=test
with the following rewrite :
RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=$1 [QSA,L]
it's working fine.
now, there are old urls still like /xxx/index.php?id=$1
and i want to get rid of these old urls, meaning I want all of them to be for the users like /xxx/test.php with a 301 redirect
i did a rewrite for this but then i'm entering a loop despite the L flag
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^xxx/index\.php$ /xxx/%1.php? [R=301,L]
? is it possible to handle that and how ?
and other to describe it is allways use the script :
/xxx/index.php?id=$1
but allways have the right url in the browser displayed
Keep your existing
RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=$1 [QSA,L]
which appears to work fine.
Add in these two lines before that which will catch if there is an id= and strip it out of the URL.
RewriteCond %{QUERY_STRING} ^id=([^&]*)(.*)$
RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=%1%2 [L,R=301]
^ start of query string
([^&])* any character except &
(.*) any following characters
So if query string is id=test&something=else RewriteRule will append exactly that and nothing else as there is no more QSA flag.
Try those 3 lines together (htaccess test website), here is the full htaccess file:
RewriteCond %{QUERY_STRING} ^id=([^&]*)(.*)$
RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=%1%2 [L]
RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=$1 [QSA,L]
Make your RewriteRule not match index.php or remove the QSA flag.
Say you type test.php well now you will go to index.php?id=test
Then Rewrite occurs again and you will go to index.php?id=index&id=test
Then it will occur again because the page is different: index.php?id=index&id=index&id=test etc.
So add in your regex a negative lookahead: xxx/(?!index)([0-9a-z\-]*)\.php
Try:
RewriteRule ^xxx/(?!index)([0-9a-z\-]*)\.php$ /xxx/index\.php?id=$1 [QSA,L]

RewriteCond when REQUEST_URI do not match htaccess apache2

I have a multilingual wordpress website and want to redirect website of given region to given language,
xyz.de --> xyz.de/de/
xyz.co.uk --> xyz.co.uk/en/
direct access to xyz.de/de and xyz.co.uk/en are working properly. So there is no problem on wordpress side.
Now, I am trying to change the htaccess file of xyz.de and xyz.co.uk so that they redirect the website.
Considering xyz.co.uk
I want to add a RewriteCond such that whenever there is no /en trailing after xyz.co.uk it will automatically add /en.
For example xyz.co.uk/<trailing address> results in xyz.co.uk/en/<trailing address>
So far I have the following code, which somehow doesn't seem to work,
RewriteCond %{REQUEST_URI} !^/en
RewriteRule ^(.*)$ http://xyz.co.uk/en/$1 [L]
The negation of /en is not working! I have also tried
RewriteCond %{REQUEST_URI} !/en
RewriteRule ^(.*)$ http://xyz.co.uk/en/$1 [L]
Could someone tell me where I am going wrong? seems like I have gone wrong in writing RegEx and suggest if there is better way to achieve the same, that does not affect the SEO across different domains.
Use THE_REQUEST variable instead of REQUEST_URI:
RewriteCond %{HTTP_HOST} \.co\.uk$ [NC]
RewriteCond %{THE_REQUEST} !/en/ [NC]
RewriteRule ^ /en%{REQUEST_URI} [L,R=302,NE]
Make sure to keep this rule as your very first rule in .htaccess.
Change it to R=301 once you've tested.

Remove symbol and text from url htaccess

I need to remove some specific text (and all characters after it from a url) - have tried several options, but I can't get any of them to work, a sample url is:
/yacht-sales/yacht/eclipse1?title=http://www.oceanindependence.com/yacht-sales/yacht/daydream&submit=
It's everything from the ?title= - that needs to be removed, so the final url would be:
/yacht-sales/yacht/eclipse1
there is another site on the same server that uses ? in it's url's, so removing everything from the ? onwards did cause problems when I tried it, and I did also try:
RewriteCond %{QUERY_STRING} ^?title=http$ [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L,NE]
But this didn't work, sorry am not really good at things like this, if anyone can help at all?
Cheers
%{QUERY_STRING} doesn't contain first ? and since its not ending with http hence don't put $ there.
You can use this rule:
RewriteCond %{QUERY_STRING} (^|&)title=http.+ [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L,NE]

Can't match RewriteRule

I need to do a very simple URL rewrite but my RewriteRule is not working.
I want this URL: http://myweb.com:8080/MySite/bla?bla
To become this: http://myweb.com:8080/MySite/index.php
My .htaccess file content is like this:
RewriteEngine On
RewriteRule bla\?bla index.php
It is located in "MySite" folder. I can do other url-rewriting rules with success but I got stuck whenever I need to write a rule with question mark inside.
What am I doing wrong?
You need to use the %{QUERY_STRING} rewrite condition for this.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^bla$
RewriteRule ^/?bla$ index.php [NC,L]
Please, note that the ? in the rewrite rule is not there to match against the ? in the query string. That part is handled completely by %{QUERY_STRING}. The [NC] just makes the rule case-insensitive and [L] marks the rule as last.