Struggling with htaccess and regex - regex

I just can't get my head around the regex required for a URL rewrite.
I want the following :
http://domain.com/?hid=yv&v=0.1.4383
to rewrite to
http://domain.com/example/
All other pages should remain unchanged.
Anyone help me out and explain the regex required for me.

You need to make sure to match against the %{QUERY_STRING} variable.:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^hid=yv&v=0\.1\.4383$
RewriteRule ^/?$ http://domain.com/example/ [L,R]

Related

htaccess rewrite rule, match URL params

I am sorry to ask a so trivial question, but I cannot find answers in RewriteRule docs. It should be something very simple that I am missing here...
I need to redirect
/index.php/component/users/?view=login
to
/index.php
if the URL matches "view=login"
I tried
RewriteRule ^((.*)\?view=login(.*))$ /index.php? [R,L]
and it doesn't work though testing it on www.regextester.com or www.regexpal.com shows a match
At the same time matching the substring "users" works perfectly fine using
RewriteRule ^(.*)/users(.*)$ /index.php? [R,L]
What is the problem with my regex matching "view-login"? Why does it work on a tester and doesn't on a live site?
Thanks!
UPDATE
I have managed to get it work using RewriteCond and RewriteRule:
RewriteCond %{QUERY_STRING} login
RewriteRule ^(.*)$ /? [R,L]
Still I cannot understand why a single RewriteRule doesn't work to match string after "?". It works only for the part of the string before "?".
I would appreciate if someone could explain that. Thanks!

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.

.htaccess rewrite rule with multiple {QUERY_STRING}s

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.

RewriteRule in htaccess having no affect

Sorry, this is probably a simple issue but I've read tons of tutorials and can't solve the issue. Here's the URL sample:
http://localhost:8106/privacy-policy/?lang=fr&dest=app
The .htaccess contents:
RewriteEngine on
RewriteRule ^privacy-policy/?lang=([a-z][a-z])&dest=app$ privacy-policy/$1 [NC,L]
When I visit the URL I don't get redirected. Any ideas?
Thanks
You can't match against the query string in a rule, you need to use the %{QUERY_STRING} variable:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^lang=([a-z][a-z])&dest=app$
RewriteRule ^privacy-policy/$ privacy-policy/%1 [NC,L]
Note that the backreference needs to be %1. If you need it to redirect the browser, you'll also need a R flag in the square brackets.
Thanks Jon Lin for the leads on the query_string and %1 edit. It didn't end up working with a copy/paste of that code but this is finally what I ended up with and it is working:
RewriteCond %{REQUEST_URI} privacy-policy
RewriteCond %{QUERY_STRING} lang=(\w+)&dest=app
RewriteRule ^privacy-policy/$ /privacy-policy/%1? [R=301,L]
Thanks again for your help on this.

Replace .php in URL with / using .htaccess

I've been searching all day for a solution to replace .php in the URL with a / using .htaccess. Most of the solutions didn't work for me at all (didn't rewrite the URL, even just to remove .php) until I found this beautiful solution on SO.
https://stackoverflow.com/a/11084526/1724376
Now my issue is that it only removes the .php but does not replace it with a "/". I've tried many things with no luck but I don't know much about htaccess and rewrite conditions, etc. I'm really hoping someone here can help me.
Just so I don't get down-voted for not having tried anything, here's one that I tried but it didn't rewrite the URL at all.
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php
Help will be truly appreciated.
EDIT: To clarify, I want www.mysite.com/contact.php to show up as www.mysite.com/contact/
Have your rule like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]