I want to redirect some URL's using htaccess file.
My htaccess files is as under:
# 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>
Redirect /old-post-type/old-parent-post/old-post-slug/ /new-post-type/new-post-slug/
***** Some More URLs *****
# END WordPress
It should redirect to http://yourwebsitedomain.com/new-post-type/new-post-slug/ but, it is redirecting to http://yourwebsitedomain.com/new-post-type/new-post-slug/old-post-slug/
"old-post-slug/" automatically added to last I don't know why?
Any suggestions accepted.
I wouldn't recommend mixing mod_alias and mod_rewrite like that, the RewriteRule runs first, then runs again after Redirect, making strange things happen. Just use mod_rewrite all the way through, with the redirect before the other rules and with L flag, like so:
RewriteEngine On
RewriteBase /
#I put 302, use 301 if permanent redirect is what you are after.
RewriteRule ^old-post-type/old-parent-post/old-post-slug/$ /new-post-type/new-post-slug/ [L,R=302]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
And this only if you really must use htaccess for the redirects, like someone mentioned wordpress already has functionality for dealing with redirects.
Try this:
Redirect 301 /old-post-type/old-parent-post/old-post-slug/ http://your-domain.com/new-post-type/new-post-slug/
This should work. The 301 stands for "Moved Permanently".
Related
I'm struggling to get my .htaccess configured properly to setup a subdomain redirect for specific subdomains. I want to redirect (not mask) a subdomain to a specific page and repeat this configuration multiple times.
I'm hosted on a Google Compute Engine currently using Cloud DNS (used to host on Godaddy in which subdomain management was very simple)
before:
# 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>
after:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^sat.theteachertutors.com$ [NC]
RewriteRule ^(.*)$ /private-sat-act-tutoring/$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I tried the config above but the redirect does not work. The target url is: https://www.theteachertutors.com/private-sat-act-tutoring/
Any guidance/tips would be most appreciated. I've search for other examples but can't get this configuration to work at all, ie: http://sat.theteachertutors.com
Thanks
You were close to the solution, change the rewrite rule to this:
RewriteRule ^(.*)$ https://www.theteachertutors.com/private-sat-act-tutoring/$1 [R=301,L]
When you are redirecting to another domain/subdomain you have to use the full URL. Also I use R=301 to mark the redirect as a permanent redirect (by default rules that point to another domain use a 302 redirect).
I have setup a website with recipes but in the process of migration OLD urls to NEW urls I'm facing issues with.
https://example.com/recipes/cheesy-baked-tacos/
This is my old URL structure and In a wordpress site I need to extract last part of the URL "cheesy-baked-tacos" and handle that in the template I use for recipes page.
Now when i click on the URL it goes to 404 page even though recipes page has setup.
I tried following htaccess rules but have no success
RewriteBase /
RewriteRule ^recipes/(.*)$ index.php?l=$1
Thanks in Advance.
Full htaccess file looks like this
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^recipes/(.*)$ index.php?l=$1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
you should check your permalink change to starts with id to without query string
dont change htacces file
you just change permalink in new server
https://example.com/recipes/cheesy-baked-tacos/
this is your old url and has permalink set without query string
new url https://example.com/recipes/cheesy-baked-tacos/id=50 just like
thats why your code have error
Managed to fix the issue with 301 Redirect. Here's what i used if it helps someone.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^recipes/(.*)$ recipe/?post_name=$1 [L,R=301,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I am trying to redirect old URLs that look like this:
mysite.com/index.php?dispatch=products.view&product_id=30176
to new URL's that are SEO friendly and look like - mysite.com/category/product
I am using CS-Cart 4.3.1 with SEO add-on enabled, and when I have imported all the products and categories the URLs were automatically changed.
This add-on has 301 redirect functionality in admin, which enables me to create 301 redirects manually. However, something is wrong and it doesn't work with redirects that have "index.php?" in a path to start with.
I have tried redirecting old URLs without "index.php?" in a path - like:
"dispatch=products.view&product_id=30176" to "mysite.com/category/product" and it works, page is being redirected. But this is not the correct URL that is indexed by Google and need redirecting.
I suspect the issue is somewhere in Rewrite Conditions or Rewrite Rules on htaccess file that were created by system upon activation of SEO add-on.
This is how it looks like:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
Options -MultiViews
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} ^api/(.*)$ [or]
RewriteCond %{REQUEST_URI} .*/api/(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*api/(.*)$ api.php?_d=$1 [L,QSA]
RewriteCond %{REQUEST_URI} \.(png|gif|ico|swf|jpe?g|js|css|ttf|svg|eot|woff|yml|xml)$ [or]
RewriteCond %{REQUEST_URI} store_closed.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)\/(.*)$ $2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]
</IfModule>
If anybody can think of a reason why the redirects are not working when "index.php?" is included in a path, I would greatly appreciate your help.
The built-in "301 redirects" feature does not support non-seo friendly URLs as old URL parameter. It means that you cannot use "index.php?dispatch=products.view&product_id=30176", but you can use "old_website_category/old_website_product" one.
To realize what you want, please add 301 redirects to the .htaccess file. E.g.
RewriteCond %{QUERY_STRING} ^product_id=30176$
RewriteRule ^/?index\.php$ http://example.com/category/product? [L,R=301]
I have the following .htaccess file
# 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]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
</IfModule>
# END WordPress
This is the link of the website.
Another domain hosted on the same server needs to be redirected to the login.php page and I am not the developer nor I have the knowledge of how that redirection is made. Yet I have to stop being redirected to the login.php. Is there a way to achieve this by .htaccess?
I actually want to enable https only on the home page and the signup page, which I can do with a HTTPS plugin. When I enable them, as I stated they go to the login.php file...
Important note: the link/domain above doesn't have an ssl configured but the other domain which needs to be redirected has it. But they have a CA bundle installed on the server.
Second important note: I actually need this to pass the data with https to securely use the fb - twitter - linkedin login buttons.
I'd be very glad for any advice. Thanks for your time!
Edit #1 - the other domain mentioned above has only these on the .htaccess
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You need your redirect rule to come Before your wordpress routing rules. Otherwise the route happens first, rewriting whatever URL you requested, then the second rule redirects the internal route.
Try:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I just migrated a site that was written quite a while ago to wordpress.
Doing this caused the url structure to be different now using mod-rewrite. I realized after I put the new site live that there are emails that have been sent out for customers to redeem their gift certificates from the site using the old URL.
My question is, with htaccess is there a url redirect that I can do and keep the get information with it. For example, I would need this url:
www.test.com/redeem.php?id=123
to redirect to:
www.test.com/redeem/now/?id=123
If there's no solution with .htaccess, does anyone have any solution for PHP?
Here's what my htaccess file looks like now:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Thanks for your help.
whether it will be like you want
http://test.com/redeem/now/read/2
the .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
using permalink wordpress
What I ended up doing was writing this code in the header file and it works.
<?php
if (array_shift(explode('?', $_SERVER['REQUEST_URI'])) == "/redeem.php") {
header ('Location: /gift-certificates/redeem/?id=' . $_GET["id"] );
}?>