.htaccess Rewrite Rule Possibility? - apache

I'm having a little issue. One of my clients recently sent out an email blast to 6500 people, that included an invalid link to a PDF file.
The link was simply: http://theirsite.com/pdf/thepdf.pdf%20
So, I'd like to be able to do an htaccess rewrite for them to the valid http://theirsite.com/pdf/thepdf.pdf file
So far, everything I've tried does not work.
Here is what I've tried thus far:
RewriteRule ^(/pdf/thepdf.pdf[%20|\s]+)$ /pdf/thepdf.pdf [R=301,L]
RewriteRule /pdf/thepdf.pdf([%20|\s]+)$ /pdf/thepdf.pdf [R=301,L]
RewriteRule /pdf/thepdf.pdf%20 /pdf/thepdf.pdf [R=301,L]
RewriteRule /pdf/thepdf.pdf%20 /pdf/thepdf.pdf [R=301,L]
RewriteRule /pdf/thepdf.pdf /pdf/thepdf.pdf [R=301,L]
RewriteRule /pdf/thepdf.pdf(.+?) /pdf/thepdf.pdf [R=301,L]
Something to note here, if I click the original link, but remove the %20 and put in a space, the rewrite works.
Just does not work with the %20

Since this is .htaccess there should not be a mandatory forward slash at the beginning of the match rule. Try this:
RewriteEngine On
RewriteRule ^/?pdf/thepdf\.pdf\s+$ /pdf/thepdf.pdf [R=301,L]
Note:
I put a /? at the beginning of the match rule as this is good general practice to make the rule work in either a host config context or an .htaccess context. Since a / would be required if this rule was in host config.
You should escape the . before .pdf otherwise it will act as a wildcard match.
I added anchors (^ and $) at the beginning and the end of the match to make sure this matches the entire resource string

%20 is a URL encoded space, so you can use a lazy select (.+?) in your .htaccess file.
RewriteEngine On
RewriteBase /
RewriteRule /pdf/thepdf.pdf(.+?) /pdf/thepdf.pdf [L]
These rules should normally work.

Related

How to prevent rewrite .htaccess file conflict problem

RewriteEngine on
RewriteRule ^([^/\.]+)/?$ category.php?slug=$1
RewriteRule ^([^/\.]+)/([^/\.]+)$ product-details.php?slug1=$1&slug=$2
RewriteRule ^([^/\.]+)/?$ infrastructure-details.php?slug=$1
what I have already tried
This is my htaccess file. problem is when I am trying to execute (infrastructure-details.php?slug=$1) its move to (category.php?slug=$1) conflict with first rule of htaccess.
I tired multiple rewrite methods but its not working. Please help to solve this issue.
localhost/project/category.php?slug=pump, localhost/project/infrastructure-details.php?slug=paint second url i want to be-> localhost/project/paint both page is different. can you please specify how to write rules for this different pages.
There is no discernible pattern that differentiates these two URLs so the only way to implement these two rewrites is to hardcode them. For example:
RewriteRule ^pump$ category.php?slug=$0 [L]
RewriteRule ^paint$ infrastructure-details.php?slug=$0 [L]
Where the $0 backreference in the substitution string contains the entire match by the RewriteRule pattern (just saves some repetition).
If you need a more general solution (as your directives suggest) then there needs to be a discernible pattern in the URL that differentiates URLs that should be rewritten to category.php and infrastructure-details.php respectively.
I'm assuming your .htaccess file, and other files, are is inside the /project subdirectory.
RewriteRule ^([^/\.]+)/?$ category.php?slug=$1
RewriteRule ^([^/\.]+)/([^/\.]+)$ product-details.php?slug1=$1&slug=$2
RewriteRule ^([^/\.]+)/?$ infrastructure-details.php?slug=$1
Rule #1 and #3 conflict - they use exactly the same pattern (regex) to match against the requested URL. The first rule is always going to "win" and rewrite the request before rule#3 is able to process the request, so rule#3 never matches.
To write a generic rule like this there needs to be a discernible difference between the URL types that you can match with a pattern/regex. For example:
/category/pump
/infrastructure/paint
And then you can construct rules...
Options -MultiViews
RewriteRule ^category/([^/]+)$ category.php?slug=$1 [L]
RewriteRule ^infrastructure/([^/]+)$ infrastructure-details.php?slug=$1 [L]
Note that the order of these directives can be important. More specific rules need to be before more generalised rules.
Options -MultiViews
RewriteEngine on
RewriteRule ^infrastructure/([^/]+)$ infrastructure-details.php?slug=$1 [L]
RewriteRule ^([^/\.]+)/?$ category.php?slug=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)$ product-details.php?slug1=$1&slug=$2 [L]
This is work fine for me. (infrastructure-details.php?slug=$1 [L]) put on top.

Redirect directory with period to query strings in htaccess

I want to have personalized urls, where a person receives a mailer, that has their purl on it, and redirect them to a landing page that receives their name via query string
The url would be like
mywebsite.com/phx/joe.smith
I would like to redirect this traffic to
mywebsite.com/youngstown/index.php?first=joe&last=smith
This should be a 301 redirect, case insensitive. I'm able to do it if the directory was /phx/firstname/lastnaname, but I need it with the dot between first and last rather than a directory.
What I have so far is
RewriteEngine on
RewriteBase /
RewriteRule ^phx\/([^\/]+)\/([^\/]+)\/? youngstown/index.php?FirstName=$1&LastName=$2 [NC,R=301,L]
RewriteRule ^phx\/([^\/]+)\/? youngstown/index.php?FirstName=$1 [NC,R=301,L]
Any help would be much appreciated!
First, you don't need to escape slashes /. Apart from that, you're almost there. The main difference is \. vs /, e.g.
RewriteRule ^phx/(.+?)\.(.+)/?$ youngstown/index.php?first=$1&last=$2 [R,NC,L]
A complete solution could be:
RewriteEngine on
RewriteRule ^phx/([^./]+)\.([^./]+)/?$ /youngstown/index.php?FirstName=$1&LastName=$2 [NC,R=301,L]
RewriteRule ^phx/([^./]+)/?$ /youngstown/index.php?FirstName=$1 [NC,R=301,L]
As Olaf says, no need to escape slashes. I am matching here on "not a dot or slash". Also adding $ to delimit the end of the match and removing RewriteBase which is not needed.
Alternative Solution
Alternatively you could use a single rule which would set an empty param for LastName when not present:
RewriteEngine on
RewriteRule ^phx/([^./]+)(?:\.([^./]+))?/?$ /youngstown/index.php?FirstName=$1&LastName=$2 [NC,R=301,L]

remove tilde (~) from address

the issue here is that we need to remove the tilde character from the address right now looks like this:
http://192.169.198.158/~mx/
and we want to end with the same address just without the tilde character (~)
this in order to create a softlynk to point this link to our server
example . com / mx /
i've found some suggestion saying that i need to insert a chunk of code into my server, one of the questions is:
the code being this:
RewriteEngine on
# Make the needed exceptions
RewriteCond %{REQUEST_URI} ^.*/icons/.*
RewriteRule ^(.*)$ $1 [PT,L]
RewriteCond %{REQUEST_URI} ^.*/error/.*
RewriteRule ^(.*)$ $1 [PT,L]
# Make /username work, remember: each paren is a $#, sequentially
RewriteRule ^/([a-z0-9]+)$ /$1/ [R]
RewriteRule ^/([a-z0-9]+)/$ /~$1/ [PT]
RewriteRule ^/([a-z0-9]+)/(.*)$ /~$1/$2 [PT]
where do i put it? in an .htaccess file? or it has to go directly into the httpd.conf file?
thanks in advance
The way the rules are written, they would only work in global or <virtualhost> context because of the leading slashes on the first argument. They would not work in htaccess without modification.

URL Rewrite rule

I trying to write "rewriting rule" in .htaccess file
I have php file on my server by name go.php
This file is use to forward/redirect to provided url/link
For example: mydomian.com/go.php?url=http://www.google.com/
It works perfect but
I want to make it like mydomain.com/?http://www.google.com/
my htaccess code
RewriteRule ^/(.*)$ go.php?url=$1 [L,QSA]
This one does not work then I tried
RewriteRule ^/?(.*)$ go.php?url=$1 [L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} .+
RewriteRule ^/?$ go.php?url=%{QUERY_STRING} [L]
QUERY_STRING is automatically carried forwarded to new URI.
The problem with the second one is that the question mark is a Regex special character that effectively means "if it exists." So the second example won't work because it's saying that the URI could start with a slash.
This is untested, but I think it should be as simple as escaping the question mark so that it's viewed as a character. Like so:
RewriteRule ^/\?(.*)$ go.php?url=$1 [L]
Note the extra slash before the question mark which identifies it as a string and not a special character.

Remove double slash at the begining of URL path

Due to moving of a site, the old hoster created a redirect to the new location. However, there is a leading slash / in the redirection and the former hoster is not able/willing to fix it. So I end up with all the redirects coming in like this:
http://sub.domain.com//path/to/file.html
So I tried to remove the leading slash:
using mod_alias
RedirectMatch 301 ^//(.*)$ http://sub.domain.com/$1
using mod_rewrite
RewriteEngine on
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
RewriteRule . %1/%3 [R=301,L]
Neither works. The latter removes multiple slashes inside the path, but not at the beginning.
There are already questions regarding slash removing, but they don't solve this problem:
Issue In Removing Double Or More Slashes From URL By .htaccess
.htaccess - how to remove repeated characters from url?
Does Apache somehow treat this case differently?
How do I get rid of one of the leading slashes?
The Problem
The point is that Apache 2 do not include leading slashes in the Requested URI, so you can't do this:
RewriteRule ^/+(.*)$ /$1 [R=301,L]
or that:
RewriteCond %{REQUEST_URI} ^/+(.*)$
RewriteRule ^.*$ /%1 [R=301,L]
what workes grat for any other kind of redirects or rewrites.
The Solution
But what you can do is to use the the Apache request variable that usually looks something like this:
GET /some-page HTTP/1.1
With this in mind we can do the following:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\s//+(.*)\sHTTP.*$
RewriteRule ^.*$ /%1 [R=301,L]
So all leading slashes will be reduced to the one that that we need.
I tested this positive on apache 2.4
This code work for me
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.*?)/{2,}([^\s]*)
RewriteRule ^ %1/%2 [R=301,L,NE]
The First / is Implied
Try this:
RewriteEngine on
RewriteRule ^/(.+) $1 [R=301,L,B,NE]
Why?
To match two slashes at the beginning of the path, in htaccess, we don't need to look for two slashes—just one—because the first slash is assumed. For instance, if you wanted to match example.com/pasta, your match rule would be ^pasta$, not ^/pasta (which would only match on example.com//pasta).
This is different from httpd.conf, where you would need to specify both slashes.
Confusing, but that's how it works.