I want to understand what is meaning of these Rewrite rules
RewriteRule ^([^/]+)/([^/]+).html /title.php?file=$1&sub1=$2 [NC]
And
RewriteRule ^admin/reg/([^/]+) /admin.php?file=$1 [NC]
Kindly give me example, how it would be redirected to title.php
If you went to http://yourdomain/yomomma/house.html it would redirect to /title.php?file=yomamma&sub1=house
The second one http://yourdomain/admin/reg/candyfloss would redirect to /admin.php?file=candyfloss
NC makes it case insensitive.
Check out this page for more information on Regex (which is what's used in the first part of the RewriteRule) Regex Quickstart. Regex basically allows you to search strings. The example you posted picks up any characters except backslashes (which are used to break your parameters anyways).
Related
I need to redirect everything from the first "/" in my domain. For example
I need to redirect this: https://audiobookscloud.com/B07D5HYCR2
For that: https://example.com/B07D5HYCR2
But I need to ensure that this only happens when there are 10 digits after the first slash, that is: ".com/"
My solution was this:
RewriteRule ([^/]+)/\d{10}?$ https://www.example.com/$1 [L,R=301]
But it doesn't work as expected.
How can I resolve this?
Your matching pattern does not match what you describe. What you implemented is that: Any string that contains any non empty string that does not contain a slash, followed by a slash and nothing else or exactly 10 digits. A RewriteRule is only applied to the path component of a requested URL. So just to the /B07D5HYCR2 in your example and not to something like audiobookscloud.com/B07D5HYCR2, as you apparently expect.
Change the matching pattern slightly to come closer to what you describe:
RewriteRule ([^/]{10})?$ https://www.example.com/$1 [L,R=301]
Though that will redirect a few more URLs than you want, according to your description. Which is why I would recommend some further changes:
RewriteRule ^/?[0-9A-Z]{10}$ https://www.example.com%{REQUEST_URI} [L,R=301]
That variant precisely matches all request paths that contain exactly 10 characters that are either a digit or a capital letter, with nothing before or behind that.
I really recommend to take a look into the documentation of the tools you are trying to use. Here that would be the apache rewriting module. It is, as typical for OpenSource software, of excellent quality and comes with great examples: https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
I want to do an url redirect to a new domain by retrieving the ID parameter but only taking the first 4 characters. Anyone know how to do this?
For example, an original url:
http://www.original.example/see/news/actualite.php?newsId=be9e836&newsTitle="blablabla"
To :
https://www.new.example/actualites/be9e
I have tested :
RewriteCond %{QUERY_STRING} ^newsId=(.*)$ [NC]
RewriteRule ^$ https://www.new.example/actualites/%1? [NC,L,R]
RewriteCond %{QUERY_STRING} ^newsId=(.*)$ [NC]
RewriteRule ^$ https://www.new.example/actualites/%1? [NC,L,R]
There are a couple of problems with this:
The regex ^$ in the RewriteRule pattern only matches the document root. The URL in your example is /see/news/actualite.php - so this rule will never match (and the conditions are never processed).
The regex ^newsId=(.*)$ is capturing everything after newsId=, including any additional URL parameters. You only need the first 4 characters of this particular URL param.
As an aside, your existing condition is dependent on newsId being the first URL parameter. Maybe this is always the case, maybe not. But it is relatively trivial to check for this URL parameter, regardless of order.
Also, do you need a case-insensitive match? Or is it always newsId as stated in your example. Only use the NC flag if this is necessary, not as a default.
Try the following instead:
RewriteCond %{QUERY_STRING} (?:^|&)newsId=([^&]{4})
RewriteRule ^see/news/actualite\.php$ https://www.new.example/actualites/%1 [QSD,R,L]
The %1 backreference now contains just the first 4 characters of the newsId URL parameter value (ie. non & characters), as denoted by the regex ([^&]{4}).
The QSD flag (Apache 2.4) discards the original query string from teh redirect response. No need to append the substitution string with ? (an empty query string), as would have been required in earlier versions of Apache.
UPDATE:
I have an anchor link (#) which is added at the end of the link, is there a possibility of deleting it to make a clean link? Example, currently I have: https://www.new.example/news/4565/#title Ideally : https://www.new.example/news/4565
The "problem" here is that the browser manages the "fragment identifier" (fragid) (ie. the "anchor link (#)") and preserves this through the redirect. In other words, the browser re-appends the fragid to the redirect response from the server. The fragid is never sent to the server, so we cannot detect this server side prior to issuing the HTTP redirect.
The only thing we can do is to append an empty fragid (ie. a trailing #) in the hope that the browser discards the original fragment. Unfortunately, you will likely end up with a trailing # on your redirected URLs (browser dependent).
For example (simplified):
:
RewriteRule .... https://example.com/# [R=301,NE,L]
Note that you will need the NE flag here to prevent Apache from URL-encoding the # in the redirect response.
Like I say above, browsers might handle this differently.
Further reading:
URL Fragment and 302 redirects
redirect is keeping hash
How to clear fragment identifier on 302 redirect?
I need to add ?lang=English to an url /self-service/more here if a visitor comes from a specific domain. (not comes from, thats impossible but i meant was first at site A and then clicked a link and went to my site)
How can i do that? I tried reading the manual but its actually a bit over my head
Try using the mix of rewrite condition and rewrite rule like this, placed on top of .htaccess
RewriteCond %{HTTP_REFERER} ^(http|https)://best-graphic-design\.co\.uk [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1?lang=English [R=301,L,QSA]
A simple explanation
First check for the referer to your site, NC flag stands for nocase/ignore case
If condition matches, redirect to desired location, in your case, same host while appending the new parameter, flags used here are R - Redirect, L - Last, QSA - Query String Append.
Hope it finally helps.
I'm trying to get the following URLs be case insensitive and to go to same spot:
http://www.mywebsite.com/test
http://www.mywebsite.com/TEST
I have the following rule:
RewriteRule ^/([a-zA-Z0-9_\-]+)/?$ /?param=$1 [L]
It works for lowercase but when I have 'TEST' after website name, it doesn't apply.
I get The requested URL /TEST.php was not found on this server. Appending .php is another URL rule that comes after the rule above. So it looks like, it doesn't match the rule and goes on to next.
Try the following:
RewriteRule ^([a-zA-Z0-9_\-]+)?$ /index.php?param=$1
RewriteRule ^([a-zA-Z0-9_\-]+)/?$ /index.php?param=$1
Explicitly referring to the index file makes it clearer. The second rule should be added if you want to match trailing slashes on your 'test' param.
This works with both http://www.mywebsite.com/test and http://www.mywebsite.com/TEST
I'm trying to get all HTML and PHP files on my site to redirect through the index.php, so that they can have common frames applied to them, a templating solution I coded that I found to be quite elegant. Anywho, I'm having issues with my PHP files, specifically those that have arguments after them.
My regular rule for PHP files is the following:
RewriteCond %{REQUEST_URI} !index.php$
RewriteRule ^(.+).php$ index.php?page=$1&type=1 [NC,L]
This works fine for any pages that have no arguments, but you can see that any PHP documents that have
?argument=something
end up as:
index.php?page=path/to/page?argument=something&type=1
which is not a working solution at all. Now, what's bothering me here is the $ at the end of the rule, shouldn't that cause it to fail if there is anything after the .php?
Anywho, I tried rewriting the rule as:
RewriteRule ^(.+).php\?(.+)$ index.php?page=$1&type=1&$2 [NC,L]
but that simply doesn't trigger at all. It seems that the regex flavor used in mod_rewrite is far different than I'm used to working with, so I'm sure these are simple mistakes I've made, but I can't seem to find decent documentation for this flavor of regex other than the most basic of examples.
Can anyone show me what I'm doing wrong? Thanks.
Try qsa in your rule, which stands for "query string append" - mod_rewrite will then append any query string from the original URL to the rewritten URL
RewriteCond %{REQUEST_URI} !index.php$
RewriteRule ^(.+).php$ index.php?page=$1&type=1 [NC,L,qsa]
RewriteRule doesn't match against the query string, which is why your second attempt did not work. Here's the relevant note from the manual
The Pattern will not be matched
against the query string. Instead, you
must use a RewriteCond with the
%{QUERY_STRING} variable. You can,
however, create URLs in the
substitution string, containing a
query string part. Simply use a
question mark inside the substitution
string, to indicate that the following
text should be re-injected into the
query string. When you want to erase
an existing query string, end the
substitution string with just a
question mark. To combine a new query
string with an old one, use the [QSA]
flag.