Redirects in .htaccess - syntax - no effect - .htaccess

Situation: after a bunch of products on an online store were renamed, the URLs needed to be changed to match the new names. Redirects were set up to handle the old vs new URL conversion.
Since the old URL's could include these model names in several various places, a "keyword" approach was taken. In other words, "if THIS_STRING occurs somewhere in the requested URL, regardless of path, redirect to this new URL".
Some redirects work, since the old model name and the new model name is different:
RewriteRule ^.*(CC88SPECIAL).*$ http://www.example.com/widgets-DD99EXTREME [L,NC,R=301]
This would properly redirect any request containing "CC88SPECIAL" to the new URL.
Problem is, the same syntax could not be applied to models where the new name included the old name, i.e. AA55COMPLETE became AA55COMPLETE100:
RewriteRule ^.*(AA55COMPLETE).*$ http://www.example.com/widgets-AA55COMPLETE100 [L,NC,R=301]
The asterisk after the "keyword" would produce a loop.
So the format was changed to:
RewriteRule ^.*(AA55COMPLETE).$ http://www.example.com/widgets-AA55COMPLETE100 [L,NC,R=301]
Problem is, this has no effect. No loop, no error, no 503, just doesn't change the URL at all.
What am I doing wrong, and what's the correct syntax to make it right? I.e. match a specific string inside the requested URL, in any position?

Your last rule failed because it does not match the requested uri /AA55COMPLETE because of the trailing . in your regex pattern. Dot in regex means to match one more character so its trying to match /AA55COMPLETEA instead . To redirect /AA55COMPLETE to /AA55COMPLETE100 you can use
RewriteEngine on
RewriteRule ^AA55COMPLETE/?$ /AA55COMPLETE100 [NC,L,R]

Related

Rewrite Url Htaccess for profile user with "." (dot) htaccess not working

I need to implement a rewrite rule for my users, this type:
https://domain/user.strike18
The problem is that using this regex, any url containing "dot" returns error.
For example
https://domain/register.php
https://domain/styles.css
So I added a dot inside the regex rule.
RewriteRule ^([#a-zA-Z0-9._-]+)$ user.php?p=profile&username=$1
RewriteRule ^([#a-zA-Z0-9._-]+)/$ user.php?p=profile&username=$1
Still not working, How do I make it work?
The issue you face is that it is impossible for the rewriting module to decide whether something like "abcde.fghi" is a user name or a file name with a "file name extension".
You could try to work around that issue by explaining the exceptions to the rewriting module, here by means of a condition that allows the application of the actual rule to only get applid of the requested path does not point to an existing file:
RewriteEngine on
RewriteCond {REQUEST_FILENAME} !-f
RewriteRule ^([#a-zA-Z0-9._-]+)/?$ user.php?p=profile&username=$1
That however has the annoying implication that this now won't work as expected for a user called for example "styles.css" ...
The issue is actually rooted not in your rewriting attempts but in how you try to publish those user profiles. You should use a unique prefix for that to prevent such unsolvable problems. So a URL like for example https://example.com/profile/user.strike18. That would obviously allow for a precise rewriting rule without any such naming collisions you currently face.

Rewrite rule in .htacess from URL with equal sign and short folder name

I need a rewrite rule in my .htaccess for the following example URL:
https://www.example.com/book=18ABCDEFG
This is the resulting URL that I need:
https://www.example.com/books/2018/ABCDEFG.pdf
I have spent a couple of hours googling and trying to solve this, but I am really stuck. If there is a rewrite wizard out there, I would really appreciate the help.
EDITED:
This is what i have come up with so far, but the only result is a 404 (not found):
RewriteEngine on
RewriteCond %{QUERY_STRING} ^book=18(.*)$
RewriteRule ^(.*)book=(.*)$ http://www.examplesite.com/books/2018/$1.pdf [R=301,L]
I was hoping that the $1 should reference the string after "18" since the only parenthesised group in the condition contains that string, but so far I haven't found the right syntax.
I should explain about the "18" too. Now the URLs are different and there will never be any other year than 2018 for book URLs with this pattern. So it can be hard coded.
But how do I reference the string after "18" in the rewrite rule?
This is what i have come up with so far, but the only result is a 404 (not found):
RewriteEngine on
RewriteCond %{QUERY_STRING} ^book=18(.*)$
RewriteRule ^(.*)book=(.*)$ http://www.examplesite.com/books/2018/$1.pdf [R=301,L]
As mentioned in comments, this is trying to match two different URLs at the same time: one where the information is contained in a query string (after an "imaginary" ?) and the other where the information is contained in the URL-path. So, it's probably not doing anything; hence the 404.
I was hoping that the $1 should reference the string after "18" since the only parenthesised group in the condition contains that string
$1 refers to the first parenthesised group in the RewriteRule pattern (of which there are two). If you want to match the first subpattern in the last matched condition then you need to use a backreference of the form %1.
However, your example does not contain a ? and therefore there is no query string. The information is contained in the URl-path instead. (Unless that is a typo in your question?! It looks like a typo, since the = is superfluous otherwise. But that would also completely change your question.)
To redirect the URL example.com/book=18ABCDEFG (ie. information in the URL-path) then your would need something like the following near the top of your .htaccess file in the document root:
RewriteEngine on
RewriteRule ^book=18([^/]+)$ /books/2018/$1.pdf [R=302,L]
If the code can only be specific characters then the regex should be appropriately specific. As it stands, it matches pretty much anything.
Test with 302 (temporary) redirects and only change to 301 (permanent) when you are sure it's working OK (if this is intended to be a permanent redirect and cached by the browser).
You will need to clear your browser cache before testing.

.htaccess, virtual directories, and semi-complex URLs

I'm basically just trying to have a master syntax for predictable URLs. Simple URL is no problem
RewriteEngine on
# RewriteRule ^friendlyUrl/content/?$ /index.php?app=main&module=content
Which to my understanding looks for the url structure and allows 1 or 0 trailing "/"'s
But some parts of the website have a /urlPrefix/ to access, eg. mysite.com/membersArea/
and /membersArea/ will be apart of every query there. I'm having trouble accomodating for trailing ?s and &s in URLs like these.
RewriteRule ^secureUrl/\?(.*)$ /index.php?app=admin&$1
This is my attempt to handle everything from mysite.com/secureUrl/ to mysite.com/secureUrl/?var1=foo&var2=bar and after many server errors and a search, I find myself here.
This is the most complex line I have and between you and me, I couldn't tell you exactly what's happening other than it looks for /friendlyUrl/10DIGITKEY/(possible task)/?possiblevars=foo&var2=bar
RewriteRule ^friendlyUrl/([a-zA-Z0-9]{10})/?([a-z]*)/?\??(.*)$ /index.php?app=main&module=web&id=$1&$2&$3
Htaccess has always been my weakest subject, and as a webmaster I pay the price constantly, any help would be appreciated.
Need to input the same request to the PHP file (plus ANY query with or without ? or &) whether its just /friendlyUrl/ or /friendyUrl/?var=1, /friendlyUrl/&var=1, /friendlyUrl/var=1
You're looking to keep the query string of your request URI to remain as is, or to be included in the rewritten URL after the rewrite process is done.
For this purpose, you use the QSA flag in your RewriteRule directive. So, to rewrite /friendlyUrl/10DIGITKEY/(possible task)/?possiblevars=foo&var2=bar, you'd have:
RewriteRule ^friendlyUrl/([a-z\d]{10})/([^/]*)/?$ /index.php?app=main&module=web&id=$1&task=$2 [QSA]
Notice the QSA flag at the end. Also, keep in mind that I'm passing the second match (the possible task of your URL) as another variable (named task). This variable will be empty if nothing was found.
QSA|qsappend
When the replacement URI contains a query string, the default behavior
of RewriteRule is to discard the existing query string, and replace it
with the newly generated one. Using the [QSA] flag causes the
query strings to be combined.

how to rewrite a custom url in joomla using htaccess

I am designing a News Website using joomla 2.5
I want rewrite this url:
http://domain.com/categoryname/?format=feed&type=rss
to:
http://domain.com/rss/categoryname
Note: I'm using mode_rewrite .htaccess for joomla.
please help me quickly.
thanks to every body in this site.
Apache's mod_rewrite allows you to transform a url to a different url utilizing regex patterns.
The pattern applies to the path and allows you to do your in your example write a regex pattern like /rss/(.+) which will match anything beginning with /rss/ and has at least one character after. The parenthesis are called a capturing group and you can reference that in the second parameter in the RewriteRule directive.
The second part /$1/?format=feed&type=rss, references the first captured group in the pattern and places it in the new url.
Finally you want to signify that it is the last rule to be processed with an [L] flag.
This gives you a rule of:
RewriteEngine On
RewriteRule /rss/(.+) /$1/?format=feed&type=rss [L]
If you intend to pass query strings to this new url, you will need to add an additional flag QSA which will result in [L,QSA] in place of [L].

.htaccess and dynamically generated SEO friendly URLs

I'm trying to build a website that may be called from the URL bar with any one of the following examples:
domainname.com/en
domainname.com/zh-cn/
domainname.com/fr/page1
domainname.com/ru/dir1/page2
domainname.com/jp/dir1/page2/
domainname.com/es-mx/dir1/dir2/page3.html
These page requests need to hit my .htaccess template and ultimately be converted into this php call:
/index.php?lng=???&tpl=???
I've been trying to make RewriteCond and RewriteRule code that will safely deal with the dynamic nature of the URLs I'm trying to take in but totally defeated. I've read close to 50 different websites and been working on this for almost a week now but I have no idea what I'm doing. I don't even know if I should be using a RewriteCond. Here is my last attempt at making a RewriteRule myself:
RewriteRule ^(([a-z]{2})(-[a-z]{2})?)([a-z0-9-\./]*) /index.php?lng=$1&tpl=$4 [QSA,L,NC]
Thanks for any help,
Vince
What's causing your loop is that your regex pattern matching /index.php. Why? Let's take a look:
First, the prefix is stripped because these are rules in an htaccess file, so the URI after the first rewrite is: index.php (query string is separate)
The beginning of your regex: ^(([a-z]{2})(-[a-z]{2})?), matches in in the URI
The next bit of your regex: ([a-z0-9-\./]*) matches dex.php. Thus the rule matches and gets applied again, and will continue to get applied until you've reached the internal recursion limit.
Your URL structure:
domainname.com/en
domainname.com/zh-cn/
domainname.com/fr/page1
domainname.com/ru/dir1/page2
domainname.com/jp/dir1/page2/
domainname.com/es-mx/dir1/dir2/page3.html
Either has a / after the country code or nothing at all, so you need to account for that:
# here -------------------v
^(([a-z]{2})(-[a-z]{2})?)(/([a-z0-9-\./]*))?$
# and an ending match here ------------^
You shouldn't need to change anything else:
RewriteRule ^(([a-z]{2})(-[a-z]{2})?)(/([a-z0-9-\./]*))?$ /index.php?lng=$1&tpl=$4 [QSA,L,NC]

Resources