URL Rewrite rule - apache

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.

Related

.htaccess with multiple slashes on the condition

I am trying to implement clean URL with the help of .htaccess in my project.
I got a 404 when i implimented a condition with multiple condition strings like keyword1/keyword2/param
all other conditions like RewriteRule ^home index.php [L,NC] works fine
My file structure be like
/subdirectory/
|-.htaccess
|-index.php
|-edit-user.php
|-new-user.php
my desired clean url is
mysite.com/subdirectory/user/edit/10
and it should translated into
mysite.com/subdirectory/edit-user.php?id=10
Some of the closest solutions i tried so far (but no luck)
RewriteRule (.*)/user/edit/([0-9]+)$ edit-user?id=$1 [L,NC]
RewriteBase /user/
RewriteRule ^user\/edit\/([0-9]+)$ edit-user.php?id=$1 [L,NC]
Any suggestions are highly appreciated.
RewriteRule (.*)/user/edit/([0-9]+)$ edit-user?id=$1 [L,NC]
Since the .htaccess file is inside the /subdirectory then you would need to write the directive like this:
RewriteRule ^user/edit/(\d+)$ edit-user.php?id=$1 [L]
And remove any RewriteBase directive.
\d is simply a shorthand character class for [0-9].
The RewriteRule pattern matches against the relative URL-path (no slash prefix). That is relative to the directory that contains the .htaccess file. You were also missing the .php extension on the filename you are rewriting to. You do not need the NC flag unless you really do want to allowed a mixed-case request, but that opens you up to potential "duplicate content" which would need to be resolved in other ways.
RewriteBase /user/
RewriteRule ^user\/edit\/([0-9]+)$ edit-user.php?id=$1 [L,NC]
Actually, you are very close here, but the RewriteBase directive would have caused this to fail. The sole purpose of the RewriteBase directive is to override the directory-prefix that is added back on relative path substitutions. The RewriteBase directive sets the "URL-path" (as opposed to filesystem path) that is added back.
So, in this example, RewriteBase /user/ would result in the request being rewritten to /user/edit-user.php?id=10 (relative to the root), which is clearly wrong based on the file structure you posted.
Without the RewriteBase defined then the directory-prefix is added back, which results in the rewrite being relative to the directory containing the .htaccess file.
Also, there's no need to backslash-escape slashes since there are no slash delimiters to the regex. (The spaces that surround the argument are the delimiters.)
all other conditions like RewriteRule ^home index.php [L,NC] works fine
Careful with this, as this will also match /homeanything and /home/something etc.
Finally found the issue.
My .htaccess was
RewriteRule ^home index.php [L,NC] RewriteRule ^([^\.]+)$ $1.php [NC]
RewriteRule ^user/edit/(\d+)$ edit-user?id=$1 [L]
(1st line to add .php to anything that comes in, and the 2nd line to convert the desired URL I needed)
What happened here is, when I try to access the URL mysite.com/subdirectory/user/edit/10
The first rule converts that into mysite.com/subdirectory/user/edit/10.php instead of mysite.com/subdirectory/edit-user.php?id=10
This causes the 404 error.
Now I changed the order and the new .htaccess file looks like,
RewriteRule ^admin/edit/(\d+)$ edit-admin.php?aid=$1 [L]
RewriteRule ^([^\.]+)$ $1.php [NC]
So, when a URL comes in, it will check into all other rules before its matches against the last rule(which appends .php) and translate into the desired result.
Lesson learned: Order matters a lot in .htaccess

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.

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.