How to append a trailing slash to URL using htaccess? - apache

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.

Related

htaccess file format gives internal server error on some redirects

I have a .htaccess file with the following content;
My .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/?$ https://meervoormamas.nl [R=301,L]
RewriteRule ^/dossiers/autisme/?$ https://meervoormamas.nl/kind/ontwikkeling/ R=301,L]
RewriteRule ^/schoolkind/opvoeding/7\-tips\-om\-uw\-kinderen\-te\-begeleiden\-op\-sociale\-netwerken/?$ https://meervoormamas.nl/kind/opvoeding/7-tips-om-uw-kinderen-te-begeleiden-op-sociale-netwerken/ R=301,L]
</IfModule>
Where the first RewriteRule does work, if I type in https://mamalove.nl it redirects to https://meervoormamas.nl
But with the second RewriteRule(a category in WP) gives an Internal server error, and also with the third RewriteRule (a post).
What am I doing wrong?
As well as the missing opening [ that delimits the flags (3rd) argument on the RewriteRule directive, as mentioned in comments, in a directory (.htaccess) context the URL-path matched by the RewriteRule pattern does not start with a slash, so these rules will never match.
Other minor issues:
You are missing the trailing slash after the domain in the first rule's substitution string.
No need to backslash-escape literal hyphens when used outside of a regex character class (3rd rule).
The RewriteBase directive is not being used here.
The <IfModule> wrapper is not required.
Try the following instead:
RewriteEngine On
RewriteRule ^$ https://meervoormamas.nl/ [R=301,L]
RewriteRule ^dossiers/autisme/?$ https://meervoormamas.nl/kind/ontwikkeling/ [R=301,L]
RewriteRule ^schoolkind/opvoeding/7-tips-om-uw-kinderen-te-begeleiden-op-sociale-netwerken/?$ https://meervoormamas.nl/kind/opvoeding/7-tips-om-uw-kinderen-te-begeleiden-op-sociale-netwerken/ [R=301,L]
Since you appear to be redirecting to a similar URL-path in the 3rd rule, this can be "simplified" by avoiding repetition. For example:
RewriteRule ^school(kind/opvoeding/7-tips-om-uw-kinderen-te-begeleiden-op-sociale-netwerken)/?$ https://meervoormamas.nl/$1/ [R=301,L]
$1 is a backreference that contains everything in the captured group in the preceding pattern. It basically just removes the "school" prefix and ensures the redirected URL ends in a trailing slash.
If these rules are being used on an existing WordPress site then these directives must go at the top of the .htaccess file, before the # BEGIN WordPress section.

Rewrite a URL to include an extra parameter with a space in it

Now that .htaccess rewrites all URLs and includes a parameter, how can that parameter have a space in it?
For example
http://old.io --> http://new.com?pa=v%20a
While %20 works when testing in a .htaccess simulator, it doesn't work on my DreamHost account.
http://new.com?pa=v%20a --> http://new.com?pa=v0a
http://new.com?pa=v\ a --> http://new.com?pa=v a
http://new.com?pa=v%2520a --> http://new.com?pa=v520a
adding the NE flag doesn't change anything
You're right; Even though adding %20 works in the simulator, it does not work on a real httpd server.
To add a space, you need to escape it with a \. Here's what your htaccess file looks like:
RewriteEngine on
RewriteBase /
# Check if the host matches old.io,
# You might want to add www\. to that.
RewriteCond %{HTTP_HOST} ^old\.io
# Rewrite URLs with empty querystring
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://new.com/$1?pa=v\ a [L,R]
# Rewrite URLs with non-empty querystring
RewriteCond %{QUERY_STRING} ^.+$
RewriteRule ^(.*)$ http://new.com/$1?pa=v\ a&%{QUERY_STRING} [L,R]
Notice pa=v\ a in RewriteRule directives.

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.

.htaccess same url with or without /

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]