Everything I can find for query string redirects talks about something like this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^view=home$
RewriteRule .? http://mywebsite.com/? [L,R=301]
That's great, but I'm dealing with a url that looks like this:
example.com/portfolio/?gal=16
If I test it without the "portfolio/" it will work fine, but my old site is indexed as above. Any tips on how I can make it work with my URL structure?
EDIT Current .htaccess contents
# 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
# Canonical Remove www
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
# Redirects
Redirect 301 /feedback http://example.com/testimonials
Redirect 301 /about http://example.com/about-us
Redirect 301 /contact http://example.com/contact-us
#futile attempts at query string redirects omitted
Thanks.
New Edit to include #anubhava's suggestions
Might as well give you the actual .htaccess content since the site is now live.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^dev.bayshorephotography.com$ [OR]
RewriteRule (.*)$ http://bayshorephotography.com/$1 [R=301,L]
# Canonical Remove www
RewriteCond %{HTTP_HOST} ^www.bayshorephotography.com$ [NC]
RewriteRule ^(.*)$ http://bayshorephotography.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^gal=16$
RewriteRule .? http://bayshorephotography.com/portfolio/weddings-02/? [L,R=301]
Redirect 301 /feedback http://bayshorephotography.com/testimonials
Redirect 301 /about http://bayshorephotography.com/about-us
Redirect 301 /contact http://bayshorephotography.com/contact-us
#301 Redirect Entire Directory to remove /blog/ from all blog pages except the main archive itself
RedirectMatch 301 /blog(/.*)/ $1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# Old Redirects that don't work
#Redirect 301 /portfolio?gal=20 http://bayshorephotography.com/portfolio-items/weddings-04/
#Redirect 301 /portfolio?gal=29 http://bayshorephotography.com/portfolio-items/our-world-03/
#Redirect 301 /portfolio?gal=16 http://bayshorephotography.com/portfolio-items/weddings-02/
#Redirect 301 /portfolio?gal=18 http://bayshorephotography.com/portfolio-items/weddings-04/
#Redirect 301 /portfolio?gal=28 http://bayshorephotography.com/portfolio-items/album-samples-01/
#Redirect 301 /portfolio?gal=19 http://bayshorephotography.com/portfolio-items/weddings-05/
#Redirect 301 /portfolio?gal=22 http://bayshorephotography.com/portfolio-items/portraits-01/
#Redirect 301 /portfolio?gal=24 http://bayshorephotography.com/portfolio-items/our-world-01/
#Redirect 301 /portfolio?gal=17 http://bayshorephotography.com/portfolio-items/weddings-03/
#Redirect 301 /portfolio?gal=36 http://bayshorephotography.com/portfolio-items/portraits-04/
#Redirect 301 /portfolio?gal=35 http://bayshorephotography.com/portfolio-items/portraits-03/
#Redirect 301 /portfolio?gal=32 http://bayshorephotography.com/portfolio-items/weddings-01/
#Redirect 301 /portfolio?gal=25 http://bayshorephotography.com/portfolio-items/our-world-02/
#Redirect 301 /portfolio?gal=23 http://bayshorephotography.com/portfolio-items/portraits-02/
#Redirect gone /portfolio?gal=34
#Redirect gone /portfolio?gal=33
#Redirect gone /portfolio?gal=27
#Redirect gone /portfolio?gal=21
You need to change order of your rules and keep 301/302 before WP rules:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Canonical Remove www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^view=home$
RewriteRule .? http://example.com/? [L,R=301]
# Redirects
RewriteRule ^feedback/?$ http://example.com/testimonials [L,R=301]
RewriteRule ^about/?$ http://example.com/about-us [L,R=301]
RewriteRule ^contact/?$ http://example.com/contact-us [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Related
I have .htaccess file with redirect 301 on it and placed right under the rewriteengine
<IfModule mod_rewrite.c>
RewriteEngine On
#now this is the redirect
Redirect 301 /blog.php?slug=konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah
Redirect 301 /blog/2021/02/11/konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah
</IfModule>
only 2nd redirect works, the one with the get method are not redirected, am i doing wrong?
EDITED :
this is my entire mod rewrite :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# Permanent URL redirect
Redirect 301 /blog.php?slug=konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah
# Permanent URL redirect
Redirect 301 /blog/2021/02/11/konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah
Redirect 301 /blog.php?slug=konsolidasi-tanah-frequently-asked-questions /blog/2021/02/11/pengertian-konsolidasi-tanah
RewriteRule ^blog/2021/02/11/pengertian-konsolidasi-tanah /blog.php?slug=konsolidasi-tanah-frequently-asked-questions
It is usually not a good idea to mix RewriteRule and Redirect 301 directives. They can conflict with each other in unexpected ways. You depend of RewriteRule so you should implement your redirects with more of them.
Redirect 301 can't redirect based on query strings (?...) in the URL, so you need to implement RewriteRules for that redirect anyway.
When you have rules for redirecting specific URLs, they should go at the top of the .htaccess file so that they take precedence over the other more general rules.
I would recommend disabling the directory index because I fear it would conflict with your RewriteRule ^index\.php$ / [R=301,L] rule.
I don't see RewriteEngine On in your .htaccess file, despite that your snippet you posted to start with has it.
Try this as your .htaccess:
# Disable index.html, index.php default functionality
DirectoryIndex disabled
RewriteEngine On
# Permanent URL redirect
RewriteCond %{QUERY_STRING} ^slug=konsolidasi-tanah-frequently-asked-questions$
RewriteRule ^blog\.php$ https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah [R=301,L]
RewriteRule ^blog/2021/02/11/konsolidasi-tanah-frequently-asked-questions$ https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah [R=301,L]
# Forward URLs without .php extension to existing PHP file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
# Redirect index.php URLs to the directory
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301,L]
# Use index.php as a front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
I've tried a few work arounds but can't seem to find a solution that works.
When I try to create a 301 redirect for a URL, the redirect seems to work, but appends a 'location' word to the end of the target URL, which is referenced in the URL that's being redirected. For example:
Redirect 301 /web-design/ecommerce/london/ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/
Results in:
https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/london/
There are other redirects in the file but I've also tried deleting them all incase they were having an impact.
Full htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,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
Redirect 301 /web-design/ecommerce/ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/
Redirect 301 /web-design/ecommerce/reading/ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/
Redirect 301 /web-design/ecommerce/marlow/ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/
Redirect 301 /web-design/ecommerce/oxford/ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/
Redirect 301 /web-design/ecommerce/newbury/ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/
Redirect 301 /web-design/ecommerce/bracknell/ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/
Redirect 301 /web-design/ecommerce/wokingham/ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/
Redirect 301 /web-design/ecommerce/london/ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/
Have every rule using mod_rewrite like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^web-design/ecommerce/?$ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/ [L,NC,R=301]
RewriteRule ^web-design/ecommerce/reading/?$ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/ [L,NC,R=301]
RewriteRule ^web-design/ecommerce/marlow/?$ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/ [L,NC,R=301]
RewriteRule ^web-design/ecommerce/oxford/?$ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/ [L,NC,R=301]
RewriteRule ^web-design/ecommerce/newbury/?$ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/ [L,NC,R=301]
RewriteRule ^web-design/ecommerce/bracknell/?$ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/ [L,NC,R=301]
RewriteRule ^web-design/ecommerce/wokingham/?$ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/ [L,NC,R=301]
RewriteRule ^web-design/ecommerce/london/?$ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/ [L,NC,R=301]
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [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
Make sure to clear your browser cache completely before testing this change.
I want to redirect all subdomains except www to https://www.example.com in .htaccess, how can I do this? All subdomains can be accessed.
I only want to redirect the subdomains, not inner pages on the main domain.
Examples: (Should redirect)
hello.domain.com
test.example.domain.com
www.hello.sub.domain.com
Examples of where not to redirect:
www.domain.com/hello-world
www.domain.com
Current .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.dk$
RewriteRule ^(.*) https://www.domain.dk/$1 [QSA,L,R=301]
# 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
This should get you started. Anything apart from www.mydomain.com will be redirected to www.example.com.
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteRule ^(.*) https://www.example.com/$1 [QSA,L,R=301]
Edit:
Remember that it's the .htaccess in your subdomain folder, or _wildcard_domain.com that you will have to create or edit for this to work.
Try this in your .htaccess:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I'm trying to rewrite all urls to https://www. I've searched and tried various rules, but with no luck
I want example.com/something/something to be redirected to https://www.example.com/something/something
It's a wordpress installation. My .htaccess currently looks like this and it doesn't have the desired effect. Could you help me with the syntax?
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=302,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
redirect 301 /tulossa-pian /
redirect 301 /vakava-2014 /
redirect 301 /omasivu /
redirect 302 /vakava-2015 /
redirect 302 /vakava-juliste /
I am currently switching from olddomain.com into newdomain.com and at the top of my .htaccess I have the following Redirect's:
Redirect 301 /pageA/ http://newsite.com/pageA
Redirect 301 /pageB/ http://newsite.com/pageB
Redirect 301 /pageCa/ http://newsite.com/pageCb
Redirect 301 /pageD/ http://newsite.com/pageD
As you can see there, one of the redirects, "pageCa" changes to "pageCb" and sometimes the 301's take precedence. If I later type the same URL it acts as if the 301's aren't there and the below catch all takes effect instead.
# Catch all for pages different from the Redirects
RewriteCond %{HTTP_HOST} ^oldsite.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.oldsite.com [NC]
RewriteRule ^(.*)$ http://newsite.com/$1 [L,R=301,NC]
What may be causing this conflict?
Both domains are under the same DocumentRoot and everything else seems to work reliably.
My full .htaccess:
AddHandler application/x-httpd-php54s .php
Redirect 301 /pageA/ http://newsite.com/pageA
Redirect 301 /pageB/ http://newsite.com/pageB
Redirect 301 /pageCa/ http://newsite.com/pageCb
Redirect 301 /pageD/ http://newsite.com/pageD
ErrorDocument 404 /404
<IfModule mod_rewrite.c>
RewriteEngine On
Options +SymLinksIfOwnerMatch
# Removes www
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# Catch all for pages different from the Redirects
RewriteCond %{HTTP_HOST} ^oldsite.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.oldsite.com [NC]
RewriteRule ^(.*)$ http://newsite.com/$1 [L,R=301,NC]
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://newsite.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://newsite.com/$1 [R=301,L]
# Maps to php files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC]
# All 404 requests
RewriteCond %{REQUEST_URI} ^/404$
RewriteRule ^(.*)$ http://newsite.com/404 [L]
</IfModule>
Ok it seems to be working with the following before the "catch all" instead of the redirects:
Where the page name stays the same, url changing:
RewriteRule ^oldsite.com/pageA/?$ http://newsite.com/pageA [L]
Where both are different:
RewriteRule ^pageCa/?$ http://newsite.com/pageCb [L]
Thanks