Here is my .htaccess:
...
rewriterule file%20name.htm http://mysite.com/new_name.htm#anchor [r=301,nc,ne]
...
It's not redirected, and I believe this is because it's not targeting the file with the %20 in the name. As for the flags, I used ne to not rewrite the #anchor portion of the redirect.
Also, the flag "nu" breaks my file and I get internal server 500 error.
Thanks you.
When mod_rewrite is invoked, the path has already been decoded as part of Apache's request processing. So, if you want to check for a space, you can do so literally:
RewriteRule file\ name\.html http://example.com/new_name.htm#anchor [R=301,NC,NE,L]
The NU flag doesn't exist, which is why you get an internal server error when you try to use it.
Not the best answer, and can only be used in specific cases, but I changed
file%20name.htm
to
file.*
so it will match any filename that starts with "file", regardless of spaces and special chars.
Related
Google Search Console is showing 404 Page Not Found error for
https://example.com/page/https://example.com/page/
and the link is coming from an external website.
I want to redirect with .htaccess:
https://example.com/page/https://example.com/page/
to
https://example.com/page/
Can anyone can help me in this regard?
Try the following mod_rewrite directives at the top of your .htaccess file:
RewriteEngine On
RewriteRule ^(.*?)https?:/ /$1 [R=301,L]
This just removes any trailing part on the URL-path that starts http:/ (or https:/).
UPDATE: The ? in the capturing subpattern (.*?) makes it non-greedy, so it only captures up to the first occurrence of https:/ and discards the rest, rather than up to the last occurrence (greedy) and looping (redirect loop) until all occurrences of https:/ were removed.
Additional notes:
First test with 302 (temporary) redirect to make sure it works. Only change to 301 when confirmed, to avoid caching issues.
The URL-path that is matched by the RewriteRule pattern has already had sequences of slashes reduced to single slashes, so you can't match // (double slash) here (but I don't think you need to).
If there are query strings involved then you may need a slightly different approach and another directive, since the query string itself (as opposed to the URL-path) might contain the "repeated URL" that needs to be removed (we would need to see an example first). The RewriteRule pattern matches against the URL-path only, not the query string.
On Windows: If the (scheme and) colon (:) appears in the first path segment (ie. the malformed link is for the document root) then Apache will generate a 403 Forbidden before .htaccess is able to redirect. There is nothing you can do to avoid this since it is a limitation of the OS (colons are not allowed in filesystem paths - the 403 occurs when Apache tries to map the URL to a filesystem path). This does not happen on Linux. For example: https://example.com/https://example.com/.
UPDATE: If you are not seeing a redirect, just a 404 then you may need to enable additional pathname information (PATH_INFO) on your URLs. For example, at the top of your .htaccess file:
AcceptPathInfo On
Songs that contain a "#" in the track title give me a 404 error while trying to download from my site. How to fix this?
This is my current .htaccess code:
RewriteRule ^download/([^/]*)-([^/]*).mp3$ download.php?download=$2&pl=$1
The # is never reaching apache. It is a jump-marker and everything in the url after # is not sent to the webserver. The javascript:document.location.hash is jumping to an anchor in the page. You can escape the # with %23 which then should reach your webserver even without htaccess. As you use php, you can use htmlentities($filename) or urlencode($filename) to fix it during output.
From the
Apache manual
By default the special characters such as ?, #will be converted to their hexcode.Using the [NE] flag prevents that from happening.
RewriteRule ^download/([^/]*)-([^/]*)\.mp3$ download.php?download=$2&pl=$1 [NE,QSA,NC,L]
The above example will redirect /download-foo-bar.mp3
to download.php?download=bar&pl=bar#1 . Omitting the [NE] flag will result in the #
being converted to its hexcode %23., which will then result in 404 not found error condition.
And in RewriteRule pattern ,dot (.) is a special characters, it matches any characters. Escape it using a backslash if you want to match a literal dot(.).
I am terrible with mod_rewrite however I need to rewrite any request to the folder /files/users/*/ (* is a wildcard) to /view/ and insert the filename into a query paramater like so:
/files/users/9/test.pdf becomes /view/?file=test.pdf
How would I go about this assuming that the .htaccess file will be located inside /files/users/?
I would really appreciate if you explained how your solution works as I am slowly trying to become familiar with mod_rewrite.
So, you wanna have all my trade secrets on a silver plate?
Well, I try my best. ;-)
First of all, you must know where the documentation is. Look here for the reference: mod_rewrite. Or mod_rewrite, if your Apache version is 2.2.
You will find an overview with lots of links at Apache mod_rewrite. There, you will find a nice introduction to rewriting URLs. Also look here for lots of standard examples.
Since mod_rewrite supports PCRE regular expressions, you might need perlre and/or regular-expression.info from time to time.
Now to your question
RewriteEngine On
RewriteRule ^(?:.+?)/(.*) /view/?file=$1
This might already be sufficient. It looks for a subdirectory (?:.+?) in /files/users and captures the name of a file (.*) in this subdirectory. If this pattern matches, it rewrites the URL to /view/?file= and appends the captured file with $1, which gives /view/?file=$1.
All untested, of course, have fun.
P.S. Additional info is here at SO at .htaccess info and .htaccess faq.
Put the directive below in your .htaccess file to rewrite /files/users/9/test.pdf to /view/?file=test.pdf. In practical terms this means that if you visit http://yourdomain.com/files/users/9/test.pdf then the visitor will be served the rewritten url which is http://yourdomain.com/view?file=test.pdf
RewriteRule ^[^/]+/(.*)$ /view/?file=$1 [L]
A RewriteRule directive is part of the Apache mod_rewrite module. It takes two arguments:
Pattern - a regular expression to match against the current URL path (note that the URL path is not the entire URL but eg. /my/path, but in a .htaccess context the leading slash / is stripped giving us my/path).
Substitution - the destination URL or path where the user will rewritten OR redirected to.
Explaining the rule
The pattern ^[^/]+/(.*)$:
^ - the regex must match from the start of the string
[^/] - match everything but forward slash
+ - repetition operator which means: match 1 or more characters
/ - matches a forward slash
(.*) - mathes any characters. The dot means match any character. The star operator means match ANY characters (0 or more). The parantheses means the match is grouped and can be used in backreferences.
$ - the regex must match until the end of the string
The substitution /view/?file=$1:
...means that we rewrite the URL path to the /view/ folder with the query parameter file. The query parameter file will contain our first grouped match from the pattern as we pass it the $1 value (which means the first match from our RewriteRule pattern).
The [L] flag:
...means that mod_rewrite will stop processing rewrite rules. This is handy to avoid unwanted behaviour and/or infinite loops.
Our current htaccess setup correctly converts urls like this: site.com/page.php?sid=Friend to site.com/Friend
However, due to an unrelated oversight, we had almost all of our URLs double-indexed as site.com/Friend> Because the greater than sign is a special character it doesn't call page.php so the > needs to be stripped out in htaccess and can't be done on page.php. Compounding matters is that the way they're indexed is as: site.com/Friend%3E which also might need to be stripped out.
What we would like is to have another directive that looks for an ending of > (or %3E), strips it off, then redirects to the variable that's there without that ending > In essence so that site.com/Friend> (or site.com/Friend%3E) still points to site.com/Friend
Thank you for your help.
Add this to the top of your rules:
RewriteRule ^/?(.*)>$ /$1 [L,R=301]
You can use > because the URI gets decoded when matching in a RewriteRule.
I've got a directory called fb and a script inside called like.php. I'd like to have the get-id passed to the like-file using mod_rewrite.
mypage.com/fb/like.php?id=5 would be mypage.com/fb/like/5
My (not working) htaccess looks like this:
RewriteEngine on
RewriteRule /fb/like/([0-9]+) /fb/like.php?id=$1
Does anyone see what's wrong here?
Try removing the slash at the beginning of your match and replace patterns like so:
RewriteRule fb/like/([0-9]+) fb/like.php?id=$1
mypage.com/ is the domain name, so the string that gets matched is fb/like/5
Also consider using the carat at the start of your match string so that it will match fb but not fffb:
RewriteRule ^fb/like/([0-9]+) fb/like.php?id=$1
Here's a short guide to mod_rewrite I've found helpful.
Edit for your follow-up question:
To match mypage.com/something/fb/like/5, you can do this:
RewriteRule ^([^/]+)/fb/like/([0-9]+) $1/fb/like.php?id=$2
This saves the first directory as $1. [^/]+ means match one or more characters that are not a slash. Put this .htaccess file in the root directory of your domain.
Alternatively, you can use the second-to-last rule and put that .htaccess file in the "something" subdirectory. Hope that makes sense.
Or you can write a rule to match simply like.php/([0-9]+) so that it'll work no matter what the directory path looks like. You can go even more generic and make this apply to any PHP file, not just like.php. It really depends on how you want your site to work.