Having some trouble with this rewrite code. I want to add a few domains (all pointing to the same server) and make it so that they have a "hidden" URL prefix being sent to the server.
Example: You type in example-one.org/contact, the server looks at it as /one/contact/
Updated code based on #anubhava's answer
I still receive a 500 Internal Server Error when using this code. I put it through an htaccess tester and it looks like it should work.
Here is the full htaccess code, including WordPress' defaults, and without using example.org:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^discoverdowntownspringfield.org$ [NC]
RewriteRule ^((?!downtown-springfield/).*)$ /downtown-springfield/$1 [L,NC]
# 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
You can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.org$ [NC]
RewriteRule ^((?!one/).*)$ /one/$1 [L,NC]
Related
I am wanting to use the following .htaccess however it works to a point but for me to access the sub pages I have to reset my permalinks and there for this section gets reset.
It seems in Wordpress currently there is only plugins to do one or another - not both of my goals.
Goals:
To force HTTP:// to HTTPS://
Include a trailing / at the end of the url
.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !wp-content\/cache\/(all|wpfc-mobile-cache)
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /subFolder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subFolder/index.php [L]
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1/ [L,R=301]
</IfModule>
WordPress makes this block in your .htaccess file. Note the first and last line (I added "subfolder/" to fit your example):
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subfolder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subfolder/index.php [L]
</IfModule>
# END WordPress
WordPress in theory shouldn't be overridding anything outside of the # Begin / #End lines. So you can place your extra code above or below it.
That being said, WordPress adds a trailing slash by default when using PostName/pretty permalinks so I don't think you'd need anything extra. And generally if you set the Site URL to have https, all generated links will be over https as well, with the notable exception of existing media/links in the content editor. For those, add this below the #End WordPress above (switch example.com to your domain):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
I am new to WordPress and I have uploaded my files into two domains, a main domain and a sub domain. In my sub domain I want to show my main domain images by htaccess. I have used below codes but couldnot get any success.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(files/profile_image/.*)$ http://example.com/$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://dev.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [NE,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
I have used above code but couldn't get any success. Can I get any help?
I know this is old, but posting an alternative simple method here:
RedirectMatch 301 /wp-content/uploads/(.*) https://live-site-to-redirect-from.com/wp-content/uploads/$1
I do this all the time with my localhost sites to grab the images from the online production site. In your sub domain .htaccess file use the following.
<IfModule mod_rewrite.c>
# Attempt to load files from production if
# they're not in our dev version
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule wp-content/uploads/(.*) \
http://example.com/wp-content/uploads/$1 [NC,L]
</IfModule>
So I have been experiencing a rather brain confusing issue that seems to have quite a few conflicting issues.
On a new website (wordpress) I have setup we had lots of redirects that had query strings in them. Now all the redirect plugins I activated could not handle these so I found topics on here that helped me manually add these to the .htaccess file.
Here is an example (see lines that are **)
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
**RewriteCond %{QUERY_STRING} unit=32x10**
**RewriteRule ^unit\.php$ /available-units/32ft-x-10ft/? [L,R=301]**
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I have a long list of these RewriteCond / Rules.
We noticed that after a period of time these manual redirects I added were all being wiped and the .htaccess file was reverting back to the normal version (see below). To prevent this, I changed the file permission to not writable.
# 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
However, since doing this I am now unable to use Hide My WP and Really Simple SSL as they both require access to the .htaccess file.
Is there anything else I can do with these redirects that won't end up being wiped ?
Perhaps I am putting them in the wrong place within the .htacess file?
Your help would be much appreciated.
Many Thanks
MORE DETAILS ADDED
This is more query string redirects that are present....
RewriteCond %{QUERY_STRING} unit=32x10
RewriteRule ^unit\.php$ /available-units/32ft-x-10ft-32x10/? [L,R=301]
RewriteCond %{QUERY_STRING} unit=4579
RewriteRule ^unit\.php$ /available-units/32ft-x-20ft-4579/? [L,R=301]
RewriteCond %{QUERY_STRING} unit=4147
RewriteRule ^unit\.php$ /available-units/unit-ref-4147/? [L,R=301]
RewriteCond %{QUERY_STRING} unit=4768
RewriteRule ^unit\.php$ /available-units/2020-range-unit-ref-4768/? [L,R=301]
Try this solution:
RewriteRule ^(.*)$ unit.php/available-units/32ft-x-10ft/?unit=32*10 [L,QSA]
I have the code below in my .htaccess to redirect all pages to https except one (/wc-api/v2), which must NOT use SSL.
It does redirect all pages to https successfully, but if I go to /wc-api/v2, it redirects me to /index.php.
# Custom Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !^on$
RewriteCond %{REQUEST_URI} !^/wc-api/v2
RewriteRule (.*) https://example.com/$1 [R,L]
</IfModule>
# End Custom Redirects
# 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
FYI The second block of redirects is required for Wordpress. I also cannot combine them into one single block or Wordpress will overwrite my changes.
When rewriting is configured in .htaccess context, after your last rule is applied, the whole process starts over again, using the internally rewritten URL as the new “input”, until no more rules match.
That means, when you request /wc-api/v2, it is not rewritten to HTTPS, because it fails your first RewriteCond, and so the rule in the next block rewrites it to /index.php internally.
Now the “next round” starts, with /index.php as input. RewriteCond %{REQUEST_URI} !^/wc-api/v2 now does result in “true”, because the REQUEST_URI is not /wc-api/v2 any more, it is now /index.php. Therefor, the RewriteRule is applied – and you are redirected to https://example.com/index.php
To avoid this, you must add another RewriteCond, which will prevent the rule from being applied for the REQUEST_URI /index.php as well:
RewriteCond %{HTTPS} !^on$
RewriteCond %{REQUEST_URI} !^/wc-api/v2
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule (.*) https://example.com/$1 [R,L]
I got an error on my server with the suggestion above so modified it slightly and found that this worked for me:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} !^/wc-api/v2
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule (.*) https://www.example.com/$1 [R,L]
# 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
Hope it works for you.
I have rebuilt a website that used some custom php to serve up individual store pages with urls like this:
http://thedomain.com/storedetails.php?storeNum=1
The new site is powered by Wordpress, and each individual store will live in a named subdirectory, like this:
http://thedomain.com/stores/gothamcity
I have been trying solutions I have found on stackoverflow that all seem to be variations on this: 301 Redirect of old url with parameters to a path without parametes
Here's an example:
RewriteCond %{QUERY_STRING} ^storeNum=18$ [NC]
RewriteRule ^/storedetails\.php$ http://www.thedomain.com/stores/gothamcity? [L,R=301]
But so far nothing works. I continue to get "page not found" 404 error pages in Wordpress.
My best guess is that something in Wordpress's part of the .htaccess is throwing this off? Here's what they include:
# 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
Can anyone help?
This should work:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^storeNum=18$ [NC]
RewriteCond %{HTTP_HOST} .*
RewriteRule ^storedetails\.php http://www.thedomain.com/stores/gothamcity? [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I suggest you use a RewriteMap though to map store numbers to names.