I'm trying to match a url using regex in a .htaccess file.
I am able to match the end of the string using "$" just fine, but when I try to match the start of the string using "^", the RewriteRule stops working.
Here is my .htaccess file:
RewriteEngine On
RewriteBase "/"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule "^example-url$" "index.php"
Without the "^" the regex will work, but is too lax. I've fiddled with this for a couple of hours now, but I'm not sure what else to try, since this very basic regex, that has worked in every regex tester I've tried.
My best guess atm is that it has something to do with what the start of the string actually is, but I'm not sure how to test this, since it should start just after the top level domain name with the "/".
EDIT:
Moving the .htaccess to the html folder made all my problems go away, the above .htaccess file now works fine. Thanks so much for the help #Riad Loukili and #P0lT10n.
I'm guessing my problems were caused by the way my virtual directories are setup.
Not sure but try
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^example-url$ index.php
I tried it on This .htaccess tester and it worked.
Possible Explanation: You've already specified the RewriteBase, no need to start the url with / in the Regular Expression.
Related
i want rewrite my url with htaccess. So, i want for example that
https://sample.com/~X6y2
has a match and it passes all behind the '~' as parameter to https://sample.com/req.php?id=[here]
is it possible with the '~'-character? so, if not, i need an alternative. Maybe '#'-Charakter, like tiktok.
So, it should match e.g.
https://sample.com/~AZaz09,
https://sample.com/~0123
but not
https://sample.com/0123,
https://sample.com/AZaz09,
https://sample.com/XY/~Z1234,...
I've tried several regex but nothing has provided the desired function :(
Best of these is (but not working, idk why):
RewriteEngine on
RewriteRule ^.*\/[\~]([a-z][^.]*)$ req.php?id=$1 [NC,L]
I'm new to regex and htaccess, so it's a bit difficult for me. I would be grateful if someone would provide a working htaccess including regex. Thx ✌🏼️
You want this solution
Check this solution
Demo: https://regex101.com/r/GJPZym/1
After a lot of trying, I've found a solution that is workin just fine.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*[\~]([A-Za-z0-9_-]*)$ req.php?id=$1 [L,NC]
where ~ can be replaced with anything u want :)
Everything has to redirect to www.domain.com. except for test.domain.com. Which will host a new version of the site for testing.
both of the domains need to look within their web directory.
I've searched stack overflow but none of the similar questions seem to provide a working solution for me. Probably because I don't understand htacces / regex that well yet.
This is the current content of my .htacces file.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
Try this for your second line (where test is your subdomain):
RewriteCond %{HTTP_HOST} !^(www|test)\.
The ! negates the match.
The brackets are a regex group.
The pipe symbol is an or.
What this should say is "match me all subdomains except www. & test."
Regex101 link here which might help explain further and gives my test data:
https://regex101.com/r/5udsER/1/
Disclaimer:
I wrote this afk so this is lacking an end-to-end test but should work.
I have been trying to work the regex out for this for a while now and I am struggling. Was hoping someone could help.
I have a website using apache mod_rewrite converting directories into get variables to 1 level. I am wanting to change this or add a seperate rule for the following
example.com/portfolio/plugins/jquery-tester
becoming
example.com/portfolio/handler.php?area=plugins&item=jquery-tester
I am trying to currently build up on php live regex but coming up trumps.
Try this:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/portfolio/(.+)/(.+)$ /portfolio/handler.php?area=$1&item=$2 [L]
Having the handler in the same directory as what you're matching can cause potential problems though (that's why the RewriteCond is there to check so you don't end up with an infinite loop).
If you have other rewrite rules, you may need to check that there aren't any conflicts.
RewriteRule ^/portfolio/[a-zA-Z\-_]+/[a-zA-Z\-_]+$ /portfolio/handler.php?area=$1&item=$2 [L]
I have in Google hundreds if not thousands of URLs that have the name of the product in it. My new e-commerce now replaces the whitespaces with hyphens when constructs the URL and I need to make an .htaccess to automatically redirect the old URLs to the new ones by replacing the whitespaces with hyphens.
The example URL I'm using is
detalle.php?titulo=Zapatillas%20Salomon%20Xr%20Mission&codigo=040-8800-072
but the number of whitespaces to be replaces can vary widely.
The last iteration of rules that I have tried is:
RewriteCond %{QUERY_STRING} ^(.*)[\s|%20](.*)&codigo=(.*)
RewriteRule detalle.php detalle.php?%1-%2&codigo=%3 [N=20]
In a tester I found online this only replaces the last whitespace and let the others without change, in my development server not even that.
I have spent almost a day with this and going nowhere, even when acording to Apache documentation this should work.
Thanks in advance.
Edit:
The solution given by #anubhava
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R=302]
worked as requested, but somehow broke the lines in my .htacces that previously had been working perfectly (minus the whitespaces)
RewriteCond %{QUERY_STRING} ^titulo=(.*)&codigo=(.*)$
RewriteRule detalle.php http://otherdomain/%1--det--%2? [R=301,L]
this is to transform the URLs with parameters into "friendly" URLs.
Edit2:
There was some kind of problem in the development server because it was in a subdirectory, I tried it on the production server and everything worked fine so I'm accepting the answer.
I put this edit just in case someone else have a similar situation.
You can use this rule as first rule in your root .htaccess to convert all spaces by hyphens.
RewriteEngine In
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R=302]
I have done some research so far, but could not find the solution. I have a client who has this old file structure on the URL:
/index.php?process=views/article.php&articleId=44102
which should redirect to this URL:
/news/title-of-article/
I found that I may need this in my htacces file:
RewriteCond %{QUERY_STRING} &articleId=44102 [NC]
RewriteRule .* /news/title-of-article/ [R=301,L]
and when I test it with the full URL, it won't redirect. But when I leave out the forward slash like this:
/index.php?process=viewsarticle.php&articleId=44102
it redirects fine. So I assume the forward slash is the problem here.
I'm not a specialist in setting up .htaccess files and this drives me crazy, because it seems that I have the right solution in plain sight, but don't know why the forward slash is preventing the redirect.
If you can't answer with the final solution, I'd appreciate to have some kind of tool (website, software, etc) where I would be able to test these rewrite conditions in detail. Maybe there is something like the regex-testers that are available for regular expressions, but just for RewriteCond and RewriteRules that I can use to figure it out myself.
Thanks in advance
Marian
Problem is that you're not stripping out QUERY_STRING In rewritten URI and because of that resulting URL also contains QUERY_STRING as process=views/article.php&articleId=44102 and it matches rule again causing redirection loop.
Change your rule to this:
RewriteCond %{QUERY_STRING} &articleId=44102 [NC]
RewriteRule .* /news/title-of-article/? [R=301,L]
? in the end will strip out existing query string.