remove tilde (~) from address - apache

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.

Related

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.

removing directory in apache mod_rewrite

I have a PHP site which replaces an ASP site, so the path structure is different.
In the URLs, I need to match http://apache.site/Cartv3/Details.asp & redirect to another location. What is the correct syntax to match that URL fragment?
I've already tried
RewriteCond %{REQUEST_URI} CartV3/results1.asp?Category=60
RewriteRule ^(.*)$ home-study/A-Levels/1/page-1 [R=301,L]
and
RewriteRule ^CartV3/Details\.asp?ProductID=1004 home-study/A-Levels/1/page-1 [R=301,L]
You meed to read more about mod_rewrite. Remember RewriteRule doesn't match query string. You attempt needs to be rewritten as:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^Category=60$ [NC]
RewriteRule ^CartV3/results1\.asp$ /home-study/A-Levels/1/page-1? [R=302,L,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
PS: ? after page-1 is a special mod_rewrite syntax to strip original query string. If you want to keep original query string in rewritten URL then take out ? in the end.
The problem here is that you are trying to match the query string, which has to be done by a separate RewriteCond. If you want the match specifically "Category=60", then you can add it as a Condition:
RewriteCond %{QUERY_STRING} Category=60
RewriteCond %{REQUEST_URI} /CartV3/results1.asp
RewriteRule .* home-study/A-Levels/1/page-1?
This will match http://example.com/CartV3/results1.asp?Category=60 and redirect. The ? at the end of the rule stops "?Category=60" being to the resulting URI.
If you don't care about the value in the query string, then you can remove the first condition.

.htaccess Rewrite Rule Possibility?

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.

Apache mod_rewrite going berserk - redirecting where it shouldn't

I have a script that echoes a meta redirect to a page called account_management.php5, but for some reason it automatically redirects from there to index.php5. My .htaccess file handles a couple of redirects automatically, for example index.html|php5 to the domain root, and that's the only place I can see this problem originating, but I don't understand why. This is my .htaccess file:
RewriteEngine On
#remember to change this to aromaclear
RewriteCond %{HTTP_HOST} !^sinaesthesia\.co.uk$ [NC]
RewriteRule ^(.*)$ http://sinaesthesia.co.uk/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php5|html)\ HTTP
RewriteRule ^(.*)index\.(php5|html)$ /$1 [R=301,L]
#translate any .html ending into .php5
RewriteRule ^(.*)\.html$ /$1\.php5
#change / for ?
RewriteRule ^(.*)\.html/(.*)$ /$1\.html?$2
#strip .html from search res page
RewriteRule ^(.*)search/(.*)$ /$1search_results\.html/search=$2
#translate product details link from search res page
RewriteRule ^products/(.*)/(.*)/(.*)$ /product_details.php5?category=$1&title=$2&id=$3 [L]
#Translate products/psorisis/chamomile-skin-cream-P[x] to productview.php5?id=1
RewriteRule ^products/.*-P([0-9]+) /productview.php5?id=$1 [L]
Wrong:
RewriteRule ^(.*)\.html$ /$1\.php5
Right:
RewriteRule ^(.*)\.html$ /$1.php5
Righter:
RewriteRule ^(.*)\.html$ /$1.php5 [QSA]
This same mistake of escaping special chars in the second param of RewriteRule is happening in other rules too, I don't know if apache will handle it, but I know you don't need it because second param is not a regexp.
Never compare to %{THE_REQUEST}, thats a weird thing to do, you don't need that. Moreover, this condition is fine without it. Just put there:
RewriteRule ^(.*)index\.(php5|html)$ $1 [R=301,QSA,L]
Now look at it:
RewriteRule ^(.*)\.html/(.*)$ /$1.html?$2
First, you are still accepting that there are references to .html files, just after trying to translate all .html to .php5, there's something wrong here.
Moreover, you are defineing as QueryString something that was originally a file path, and are not even putting it in a key. It won't work, it need some more treatment.
#strip .html from search res page
RewriteRule ^(.*)search/(.*)$ /$1search_results.html/search=$2
Wasn't it supposed to strip the .html? Because it is actually putting a .html there. Maybe as it is not an [L] it get fixed in the next loop, but you could just get all fixed right here.
#translate product details link from search res page
RewriteRule ^products/(.*)/(.*)/(.*)$ /product_details.php5?category=$1&title=$2&id=$3 [L]
This one full of .* is potentially unstable, specially delimitating the end. You should do this:
RewriteRule ^products/([^/]*)/([^/]*)/([^/]*) /product_details.php5?category=$1&title=$2&id=$3 [L]
# or:
RewriteRule ^products/(.*?)/(.*?)/([^/]*) /product_details.php5?category=$1&title=$2&id=$3 [L]
The last one looks correct, except that you should strip the special character that may be faced as a range delimiter, the "-". I don't think it work after a *, but just to be sure and correct the syntax:
RewriteRule ^products/.*\-P([0-9]+) /productview.php5?id=$1 [L]
Add this just after RewriteEngine on
RewriteLogLevel 9
RewriteLog /tmp/rw.log
Then restart the webserver. It should help you debug the problem.
Edit: Sorry, I didn't notice the .htaccess above. This will only work from the main apache configuration file.