.htaccess same url with or without / - apache

I am doing a painful rewrite of many urls on a website I am currently working on, but I have noticed a small problem:
RewriteRule ^domains/transfer$ ./cart.php?gid=11 [L,NC]
This line with navigate if I go to:
http://my-site/domains/transfer
But it won't work with a trailing /:
http://my-site/domains/transfer/
Is there a way I can change the RewriteCode to take either argument and go to the same page. It seems wasteful to have the same line twice to include a '/'
Any help would be appreciated.
Cheers

Change the line to this:
RewriteRule ^domains/transfer/?$ ./cart.php?gid=11 [L,NC]
The magic is here: /? and that allows for the preceding character, in this case the slash (/), to be optional.
If you want something to come after the transfer, then remove the dollar sign ($) which marks the end of the allowable matching.

I would recommend you to allow just one URL form, the one with or without the trailing slash, and redirect if malformed:
# remove trailing slash
RewriteRule (.*)/$ /$1 [L,R=301]
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ /$0/ [L,R=301]

Related

How to append a trailing slash to URL using htaccess?

I want to redirect from author/$username to author/$username/.
The $username is the author's username.
This is my .htaccess content :
RewriteEngine on
RewriteRule ^author/([A-Za-z0-9-\+]+)/?$ author/index.php?username=$1 [NC,L,QSA]
RewriteRule ^authorpost/([A-Za-z0-9-\+]+)/postid/([0-9]+)/?$ author/index.php?username=$1&postid=$2 [NC,L,QSA]
How do I need to change it?
So, if I understand correctly you just need to append a slash to the URL /author/<username>?
Try something like this at the top of the .htaccess file:
RewriteRule ^(author/[A-Za-z0-9+-]+)$ /$1/ [R=302,L]
The + inside a character class has no special meaning so does not need to be escaped. The hyphen (-) should be at the start or end of the character class in order to match a literal hyphen (without having to escape it).
In the following rewrite, you can now enforce the trailing slash, instead of making it optional. For example:
RewriteRule ^author/([A-Za-z0-9+-]+)/$ author/index.php?username=$1 [NC,L,QSA]
Summary
RewriteEngine On
# Append trailing slash if omitted
RewriteRule ^(author/[A-Za-z0-9+-]+)$ /$1/ [NC,R=302,L]
# Internal rewrites...
RewriteRule ^author/([A-Za-z0-9+-]+)/$ author/index.php?username=$1 [NC,L,QSA]
RewriteRule ^authorpost/([A-Za-z0-9+-]+)/postid/([0-9]+)/?$ author/index.php?username=$1&postid=$2 [NC,L,QSA]
Ideally, you should avoid using the NC flag on the internal rewrites in order to avoid potentially duplicate content. At the very least, it should be unnecessary.

Trailing slash mod_rewrite rule for certain subfolders

I am trying to get a trailing slash mod_rewrite rule to work for only certain subfolders.
I have article URLs like ////. For example an article URL may be /news/role-playing-games/new-roleplaying-game-released/. Sometimes, for whatever reason, people try the URLs without the trailing slash and they get a 404. I would like instead for apache to add a trailing slash to only the URLs that match certain subfolders.
I tried this rule, but it does not work.
RewriteCond %{REQUEST_URI} ^/(news|reviews|tutorials|guides)/(.*)$
RewriteRule ^(.*)$ /$1/ [L,R=301]
Can anyone tell me what I am doing wrong?
Thanks in advance.
Try
RewriteRule ^((news|reviews|tutorials|guides)/[^/]+/[^/]+)$ $1/ [L,R=301]
I assumed that the url structure you gave is the only one.

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.

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.