Multipule Query Strings .htaccess Redirect - string

Im a bit stuck where I need to redirect 80+ URLs' which have
content.php?111-rest-of-url
RewriteCond %{QUERY_STRING} ^?64-random-url-here-again$
RewriteRule ^content.php$ https://www.newdomain.com/random-url-here-again/? [R=301,L,QSD]
RewriteCond %{QUERY_STRING} ^?61-random-url-here$
RewriteRule ^content.php$ https://www.newdomain.com/random-url-here/? [R=301,L,QSD]
And there are around another 80 url's written out like this. However, when testing on site, no matter what URL you try...it always returns the first one in the .htaccess
From what I have been reading am I correct in assuming you can only use one query string of this kind ? if so is there away around this ?
Thanks in advance.

Ok so I managed to accomplish what I need by doing the following, hopefully it will help someone else.
RewriteCond %{REQUEST_URI} ^/content\.php$
RewriteCond %{QUERY_STRING} ^196-random-url-here-again$
RewriteRule ^(.*)$ https://www.newdomain.com/random-url-here-again/? [R=301,L,QSD]
RewriteCond %{REQUEST_URI} ^/content\.php$
RewriteCond %{QUERY_STRING} ^196-random-url-here$
RewriteRule ^(.*)$ https://www.newdomain.com/random-url-here/? [R=301,L,QSD]
Not sure what the difference is but it works :), would be grateful to hear from someone to explain the difference in what I have done so I understand it in the future.

Related

Remove last part of dynamic URL with htaccess

Can any one please help with this :
i am trying to redirect this :
https://www.example.com/site/members/user-id/page/page-id/?user_filter=points
to this
https://www.example.com/site/members/user-id/
please note that /user-id/ & /page-d/ are numbers that changes in every link.
I tried searching online and tried to come up with some thing but yet no success
Here what i tried so far
RewriteRule ^site/members/[0-9]{4}/page/$ https://www.example.com/site/members/[0-9]{4}/? [R=301,NE,NC,L]
and this approach:
RewriteCond %{REQUEST_URI} ^site/members/([0-9]{4}+)$/pages/ [NC,OR]
RewriteRule (.*) /site/members/1$ [L,R=301]
Would Appreciate Any Help On that
Thanks
You can use the following rule:
RewriteCond %{REQUEST_URI} ^/site/members/([0-9]+)/pages/ [NC]
RewriteRule (.*) /site/members/%1? [L,R=301]
Make sure to clear your browser cache before testing this.

mod_rewrite to dynamically send to sub-folder

I'm a complete novice when it comes to htaccess. I've searched for my exact problem to no avail.. perhaps I did not even know what to search for, haha.
Goodness, the title of this question might not even be appropriate!
Here goes:
I need:
https://www.example.com/1 (the 1 (one) at the end will be inserted by the user. So, it may be https://www.example.com/2057 for example. What ever the number at the end, I need it to go to:
https://www.example.com/folder1/folder2/1 (or what ever number is inserted.)
The closest I've come to is this:
RewriteRule ^/(.*) folder1/folder2/%1/index.php? [R,L]
However, this gives a VERY long example.com/folder1/folder2/folder/1/folder2.....for a LONG time.../1.php
Any help would be greatly appreciated.
This seems to work in may case:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*) http://www.examplesite.com/folder1/folder2/$1 [R=301,L]
This worked at time of this posting. Hope it helps someone in the future.
RewriteEngine on
RewriteRule ^([0-9]+)((?:A|L)[0-9]R[0-9]+[A-Z]?)$
https://www.mywebsite.com/folder/$2&login=$1 [L]
RewriteRule ^([0-9]+)((?:A|L)[0-9]?)$ https:/www.mywebsite.com/folder/$2&login=$1 [L]
RewriteEngine on
RewriteRule ^([0-9]+)$ https:/www.mywebsite.com/folder/$1 [R=301,L]

.htaccess Rewrite Query String

I have troubles redirecting query strings and I really can't find a way how to do it. Let me be more specific:
I want to achieve the following: Redirect http://blablabla.com/default.asp?Kategori=1&Valg=77 to: http://blablabla.com/default_Kategori_1_Valg_77.html
Any help will be very appreciated.
Kind Regards,
Pavel Nikolaev
This should work :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^Kategori=1&Valg=77$ [NC]
RewriteRule ^default\.asp$ http://blablabla.com/default_Kategori_1_Valg_77.html? [L,R]
I would like to provide a new version of what starkeen advised me:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^Kategori=(.*)&Valg=(.*)$ [NC]
RewriteRule ^default\.asp$ http://blablabla.com/default_Kategori_%1_Valg_%2.html? [L,R]
The difference is that know I am catching the values of the Kategori and Valg parameters and use them in the new URL. Really interesting!
Thank you all.

mod_rewrite causes 500 server error/Fails/works partially

I am trying to do a permanent redirect from my old design (www.your-translations.com) to my new design at www.your-translations.com/_YT.
In the process, the urls have to change from this format:
www.your-translations.com/my_page.php
to this format:
www.your-translations.com/_YT/index.php?pge=my_page
I have made many attempts so far, the latest of which is:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*your\-translations.com.*$ [NC]
RewriteRule ^(.*?)/([^/]*)\.php $1/_YT/index.php?pge=$2 [R=302]
RewriteCond %{HTTP_HOST} ^your-translations.com [NC]
RewriteRule ^(.*)$ http://www.your-translations.com/$1 [R=302,L]
This is one of my better tries in that it apparently does almost nothing (previous tries have resulted in 505 errors and infinite loops). The rewriting of URLs to "www." works properly.
I have tested the regex in different regex testers and it seems to do what I expect it to do.
Can someone explain what I am doing wrong?
WHAT exactly is the pattern applied to? Is it what is matched by the RewriteCond? The URI as I typed it in? The relative path? The actual path on the server?
Is there any way to display the string before I try to match it?
Several suggestions found in tutorials result in infinite loops on my server. Do all versions work the same way?
Unfortunately, the webhost technical support doesn't seem to know anything about mod_rewrite, so I can't expect any help from them.
According to my FTP log, I have already made about 80 attempts and I could really use a hand here.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^your-translations\.com$
RewriteCond %{REQUEST_URI} !^/_YT/([a-z0-9-_]+)\.php$
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)\.php$
RewriteRule ^(.*) /_YT/index.php?pge=%1 [QSA]
RewriteCond %{HTTP_HOST} ^www\.your-translations\.com$
RewriteCond %{REQUEST_URI} !^/_YT/([a-z0-9-_]+)\.php$
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)\.php$
RewriteRule ^(.*) /_YT/index.php?pge=%1 [QSA]
In RewriteCond, you must type a backslash before a dot and not before dash, because a dot in .htaccess means any character, so you need bslash to call is as dot. Sorry, I'm in mobile, so I couldn't explain more, but feel free to ask what you want to know about the code.
Thanks to some explanations from webmasterworld, I was able to fix my mod_rewrite code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^your-translations.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.your-translations.com [NC]
RewriteCond %{REQUEST_URI} !="^.*?/.*"
RewriteRule ^([^/]*)\.php$ http://www.your-translations.com/_YT/index.php?pge=$1 [R=302,L]
RewriteRule ^$ http://www.your-translations.com/_YT/ [R=302,L]
This seems to work correctly so far (touching wood). Thanks Servant for your help.

Rewrite specific URL to different directory

Im trying to redirect this,
example.com/slide?page=4 to example.com/slide/issue43?page=4
But it cannot effect other URL's like, example.com/slide/issue57?page=4
Im really stuck, these regular expressions are so weird. Here's the rewriterule that I've come up with,
This is not working
RewriteRule ^slide?page(.*)$ http://example.com/slide/issue43?page=$1 [L,R=301]
I need to target 'slide?page=X' specifically and have it redirect or point to 'slide/issue43?page=X'
This should work for you:
RewriteCond %{REQUEST_URI} ^/slide$
RewriteCond %{QUERY_STRING} ^page=(.*)$
RewriteRule ^(.*) /slide/issue57?page=%1 [R=301,L]

Resources