Very new to Apache here and htaccess rules. Please assume I'm doing something fundamentally wrong here. This is currently trying to be achieved currently on MAMP pro.
I'm trying to force all URL's to redirect to a lowercase version if the URL has any uppercase letters in it.
The solutions I'm finding are not reaping any results. .htaccess markup is as follows:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# PROBLEM HERE: apparently this works
CheckSpelling on
CheckCaseOnly on
# END PROBLEM
# 301 redirect with trailing slash
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
And when it comes to adding this to the httpd.conf:
RewriteEngine On
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]
the server then does nothing. I've attempted wrapping it in <Directory> and <Virtualhost> but to no avail.
Related
I have web page created in Wordpress v5.4, Theme Twenty Twelve and just installed SSL certificate.
I want to have such redirection structure (redirection codes in rectangles), to optimize for SEO:
but according to Google Chrome Site Inspector I have the follwing one:
So the schema is not fully optimized as for two cases there are dwo cascade redirections whereas should be one.
What is weird, current schema doesn't reflect .htaccess file. My .htaccess file looks as follows:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
So, my understanding of the code is that if url is typed in without https://, script rewrites it into version with https:// an nothing else, so there must be another place where other redirections (e.g. with www -> without www) are being made.
I have checked index.php - no redirections.
I also checked additional lines in .htaccess file don't overwrite those "hidden" ones, so if I simply add them I got multiple redirections error in web browser.
Could you please advise me what else should I do to get desired structure of redirections?
First rule redirect all www to https://domainname.com/, second all http to https://domainname.com/, one hop
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://domainname.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://domainname.com/$1 [R=301,L]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I’m guessing the redirect from www to non-www is due to the address you’ve set in your settings (WordPress dashboard → Settings → General).
For the redirect you want to achieve, you have to add rewrite conditions for the www. in your .htaccess
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
I need to remove trailing slash from URLs ending with .xml/ only ..
For this purpose I've created a Rewrite Condition and Rule which is working perfectly fine for the test link http://website.com/test.xml/
Test Link: http://htaccess.mwl.be?share=6fe08232-438a-53fa-8f1a-1f7f69b77b6f
The problem is when I place the rule in WordPress .htaccess file, it doesn't work at all! Seems like WordPress or YOAST Permalink structure is overriding the rule .. please help!
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} /(.*).xml/$
RewriteRule ^ /%1.xml [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Use the following code in your htacess file:
RewriteRule (.+)/$ $1 [R=301,L]
RewriteCond %{REQUEST_URI} /(.*).xml/$
RewriteRule ^ /%1.xml [L]
If not working then download new wordpress and try in that.
A friend of mine asked for help about his wordpress site. He did his work under sub.domain.com and now he'd like to have domain.com redirected to it, if possible without showing sub in the url bar
This is his domain root .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Following some resources I found, I inserted these two lines after the RewriteEngine On:
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R]
But it doesn't seem to work, it should be so simple but I am doing clearly something wrong...
I tried also commenting the rest, it didn't work
I am not an Apache expert at all, what's the problem?
To redirect example.com/ to sub.example.com/ ,you can use the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^$ http://sub.domain.com/$1 [L,R]
^$ matches the root / of example.com ,if you want to redirect everything to subdomain ,change the pattern to ^(.*)$ .
I have wordpress permalinks set up to /%postname% (note there is no trailing slash at the end).
However, wordpress is not redirecting any given page URL from the version with the trailing slash to the non trailing slash version. Any website header checking tool would return a '200' (OK) code for both versions (with and without the trailing slash). I am concerned this will have negative SEO consequences due to duplicate content. Therefore I want to have only one version of each page.
I am trying to modify the .htaccess file located on the wordpress directory so it removes the trailing slash at the end of any URL pretty much redirecting to the non trailing slash version of the URL.
I have Wordpress installed on a sub directory not on the root of my site i.e.
http://mywebsite.com/wordpress/
I tried the following code which I compiled by reading different questions posted on this website:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
The problem then is that I cannot login to wordpress admin. When I enter my user name and password on mywebsite.com/wordpress/wp-login.php
it typically used to redirect and enter to mywebsite.com/wordpress/wp-admin/
But now since the trailing slash is being stripped away from /wp-admin/ by the code I added to .htaccess then I get an error and cannot login to Wordpress.
I kept reading over this forums and then tried the following by playing around with the code and now I can login to wordpress but now the trailing slash is not being removed from any given URL.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/wp-admin/
RewriteCond %{REQUEST_URI} ^/wp-login\.php(.*)$
RewriteCond %{REQUEST_URI} ^/wp-admin$
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
Can anyone recommend what else I could do?
Pretty much all I want if for page URLs not to have a trailing slash at the end of the URL with the exception of the wordpress login page (mywebsite.com/wordpress/wp-login.php) and mywebsite.com/wordpress/wp-admin/
Keeping in mind the Wordpress install is on mywebsite.com/wordpress/ and not at root level.
Thanks so much!
Try these rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteRule ^((?!wp-admin/).+?)/$ /$1 [NC,NE,L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
I want to strip out the ?q= from my old drupal links for my site now in Wordpress
I need help with two things.
First, how do I specify a range of words instead of numbers, as in the snippet below? Once I do that will this code work to redirect ".../?q=postname" to simply ".../postname"?
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([0-9]+)$
RewriteRule .* http://www.mysite.com/?q=%1 [R=301,L]
Secondly, once i have that code working, how do I integrate it with the default Wordpress mod_rewrite (which is below)?
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Try this:
RewriteCond %{QUERY_STRING} ^q=(\w+)$
RewriteRule .* http://www.mysite.com/%1? [R=301,L]
BTW I would use [R=302,L] at first until I make sure everything works as expected. If you use 301 with wrong RewriteRule browser will cache the redirect and any further RewriteRule changes would require you to restart browser for each code change to see any difference.