Remove trailing slash in WP multisite .htaccess file - .htaccess

I've created an .htaccess file that contains redirects for one site that is part of a larger WordPress Multisite install.
The .htaccess content starts with this (necessary because the same .httacess file must be used for multiple sites:
RewriteCond %{HTTP_HOST} ^mydomain.com [nc]
And then contains a series of rewrites, like so:
RewriteRule ^about-my-site$ about [R=301,NC,L]
If I visit mydomain.com/about-my-site, I am correctly redirected to mydomain.com/about
However, if I visit mydomain.com/about-my-site/ (please note trailing slash), I get a "Page not found" error.

Change your RewriteRule to
RewriteRule ^about-my-site/?$ about [R=301,NC,L]

These rules should go before your WordPress rules, make sure that rewriting is on, and set a rewrite base of / -
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} mydomain.com [NC]
RewriteRule ^about-my-site/? about [R=301,NC,L]
</IfModule>
Test here: http://htaccess.madewithlove.be/
input url
http://mydomain.com/about-my-site/
output url
http://mydomain.com/about
debugging info
1 RewriteEngine On
2 RewriteBase /
3 RewriteCond %{HTTP_HOST} mydomain.com [NC] This condition was met
4 RewriteRule ^about-my-site/? about [R=301,NC,L] This rule was met, the new url is http://mydomain.com/about
Test are stopped, because of the R in your RewriteRule options. A redirect will be made with status code 301

Related

.htaccess rewrite to same alias without infinite redirects

I have...
| .htaccess : (v1)
RewriteEngine on
RewriteRule ^in?$ login.php
So, /in --is-really--> /login.php
This much works great. We all can learn how to do this from: .htaccess redirect with alias url
But, I want it to also work in reverse...
If someone should enter /login.php into the address bar, I want it to change to /in.
So also, /login.php --rewrites-to--> /in
From this Answer to a different Question, I want to be ready for anything, using REQUEST_URI. So, my .htaccess file starts with this...
| .htaccess : (v2)
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]
# "in" --> login.php
RewriteRule ^in?$ login.php
That also works great.
But now, I want to add this rule (my Question here) for /in <--> /login.php both ways, just how / <--> /index.php already works with .htaccess (v2). So, I adopted the settings and added a second rule...
| .htaccess : (v3) —not working!
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]
# "in" --> login.php, and also redirect back to it
RewriteCond %{REQUEST_URI} ^/(.+/)?login\.php
RewriteRule (^|/)login\.php(/|$) /%1in [R=302,L]
RewriteRule ^in?$ login.php
...but then /in and /login.php both cause an infinite redirect loop.
What's the right way to do this, still using REQUEST_URI, and still having both rewrite rules (for index.php and for login.php)?
These Questions did not help:
Rewrite rule to hide folder, doesn't work right without trailing slash
This is not about a trailing slash
Allow multiple IPs to access Wordpress Site Admin via .htaccess
This is not about IP-based access
Htaccess URLs redirects are working for http not all https
This is not about https vs http
Rewrite-rules issues : .htaccess
This is not about cleaning up the GET array in the URL
apache htaccess rewrite with alias
This is not about rewriting the host/domain, thereby preserving the path
rewrite htaccess causes infinite loop?
This is not about www subdomain rewrites
.htaccess rewrite page with alias
This is not about rewriting "pretty" URLs nor about how to use slug settings in WordPress
Htaccess alias or rewrite confusion
This is not about simply having multiple rules with the same destination
htaccess rewrite to include #!
I'm not trying to rewrite #!
Reason of redirect loop is a missing RewriteCond %{ENV:REDIRECT_STATUS} ^$ before first redirect rule that removes index.php. Remember that RewriteCond is applicable to immediate next RewriteRule only.
Suggested .htaccess:
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php$ [NC]
RewriteRule ^ /%1 [R=301,L]
# "in" --> login.php, and also redirect back to it
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?login\.php$ [NC]
RewriteRule ^ /%1in [R=302,L]
RewriteRule ^in?$ login.php [L,NC]
It won't cause redirect loop because after first rewrite to /login.php, variable REDIRECT_STATUS will become 200 and then the RewriteCond %{ENV:REDIRECT_STATUS} ^$ will stop redirect looping.
Thanks to the help from the user with the correct answer, I found that...
RewriteCond %{ENV:REDIRECT_STATUS} ^$
...doesn't go in .htaccess only once, but every time on the line before...
RewriteCond %{REQUEST_URI} ...

How to redirect from /senturia-nam-sai-gon/?gallery=170 to /senturia-nam-sai-gon/projects/nha-pho-thuong-mai-5mx12m/?

I want to redirect https://senturia.com.vn/senturia-nam-sai-gon/?gallery=170
to https://senturia.com.vn/senturia-nam-sai-gon/projects/nha-pho-thuong-mai-5mx12m/
I tried this .htaccess file
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} gallery=170 [NC]
RewriteRule (.*) /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/? [R=301,L]
</IfModule>
But it's not work
Your configuration looks fine. I highly recommend making sure that mod_rewrite is enabled on the server. Also, make sure that the .htaccess file is located in the correct place. If your are using some CMS with a single index.php file, the .htaccess should sit on the same level as that file. If senturia-nam-sai-gon is an actual directory on your server with its own index.php file, then the .htaccess should sit in that folder.
Here is how I would do it in a CMS:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (?:^|&)gallery=170 [NC]
RewriteRule ^senturia-nam-sai-gon/ /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/? [R=301,L,NC]
</IfModule>
Detailed break-down:
RewriteEngine on simply enables URL rewriting
RewriteBase / sets the base of all URLs (in this case simply /)
RewriteCond %{QUERY_STRING (?:^|&)galler=170 [NC] applies the following rule, if the GET parameters contain gallery=170. The [NC] flag makes the condition ignore upper- and lower-case
RewriteRule ^senturia-nam-sai-gon/ /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/? [R=301,L,NC] redirects /senturia-nam-sai-gon/ to /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/ with a status-code of 301 ("Moved permanently"). The ? at the end of the rule removes the querystring entirely. The L flag makes sure this is the last rule that gets applied. The NC flag does the same as before.

Rewrite only one specific url

I want to rewrite one specific url.
http://example1.com should be http://example2.de .
But http://example1.com/subdir or http://sub.example1.com should remain the same.
I found the following, which successfully rewrites example1.com, but also every url which starts with example1.com
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Background: I want to redirect the main page of an WP-Multisite but want to make sure that I can work with the backend of wordpress and run other multisites which are subdomains.
For matching only http://example.com domain (without possibility to add anything before or after the example.com) use the following code:
RewriteCond %{HTTP_HOST} ^(example.com(\/{0,1})){1}$
RewriteRule http://example2.de(\/{0,1}) [R=301,L]
That (\/{0,1}) part is for matching both example.com and example.com/ (but nothing esle) - if you do not wish to match example.com/ remove that part from both rows.
You're pretty close but you don't need to capture URI in $1:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example1\.com$ [NC]
RewriteRule ^$ http://example2.de/ [L,R=301]

Redirect url to home page with trailing dots

Google webmaster showing some duplicate url,
They are
www.abc.com/index.php?option=com_toys&view=detail&n_id=148&ite..
www.abc.com/index.php?option=com_toys&view=detail&n_id=156&item..
www.abc.com/index.php?option=com_games&view=detail&vid=170&itemid..
www.abc.com/index.php?option=com_play&view=detail&vid=175&it..
To remove them - i feel the best way is to redirect to home page for any url containing .. at end of url
tried putting this condition, but it does not work too
RewriteRule ^(.*)\.htm$ http://www.abc.com/$1 [R=301,L]
RewriteRule ^(.*)$ http://www.abc.com/$1 [L,R=301]
RewriteRule ^(..*)\.htm$ http://www.abc.com/$1 [R=301,L]
Correct url structure are
www.abc.com/index.php?option=com_toys&view=detail&n_id=148&Itemid=2
www.abc.com/index.php?option=com_toys&view=detail&n_id=156&Itemid=2
www.abc.com/index.php?option=com_games&view=detail&vid=170&Itemid=3
www.abc.com/index.php?option=com_play&view=detail&vid=175&Itemid=4
any suggestions pls ... many thnx
Edit on 13th Sep
Hello Anubhav,
If we have redirect these URL to 404 page then is below command in htaccess correct
RewriteCond %{THE_REQUEST} \?.+?\.\.
RewriteRule ^index\.php$ - [NC,L,R=404]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \?.+?\.\.
RewriteRule ^ /? [R=301,L,NE]

.htaccess: rewrite condition and rewrite problems

I am working on a CakePHP project in shared hosting with multiple subdomains. Due to problems with Cake's htaccess I had to move the main site into a subfolder, and write a new htaccess to redirect users to this folder (while leaving the subdomains requests intact). At the minute my htaccess file looks something like:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteRule (.*) /domain/$1
</IfModule>
This works for requests with 'www' prepended to the url, but there are some issues with http: // domain.com requests. In IE & Chrome this address resolves itself to the 'www' url, but in Firefox & Safari, it shows the directory structure.
I need to figure out how to include the http: // domain.com requests in the rewrite conditions without affecting the other sub-domains.
Any advice would be greatly appreciated. Thanks. Adrian
Change your %{HTTP_HOST} rewrite condition as
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/domain/ [NC]
RewriteRule (.*) /domain/$1 [L]
</IfModule>
The ? in (www\.)? says zero or one occurrence i.e. makes it optional.
[L] marks it as last i.e. rewriting should stop at this rule.
[NC] makes all matches case-insensitive.
Try this for the host match:
^(www\.)?domain\.com$

Resources