HTAccess ModRewrite not working for directory to file - wordpress

I'm trying to create a rewrite rule in htaccess to point a directory at a file, I keep getting 404 errors in return. I should mention, the main site is a wordpress site and I'm trying to redirect one of it's url's internally for a page with a trailing folder to another url.
For instance, this is the url for the wordpress page:
/homes-for-sale-details/4420/217007490/177/
The landing page I'm trying to create within that url is:
/homes-for-sale-details/4420/217007490/177/Fb-landing-page
So I did this:
RewriteRule /homes-for-sale-details/4420/217007490/177/Fb-landing-page/(.*)$ /propertylisting1/index.php?success=$1
No go, I get a 404 error in return. The wordpress page still work, but this won't.
Right now this new rule is placed after wordpress' own rewrite:
RewriteRule . /index.php [L]
I've also tried placing it before wordpress' rewrite and it breaks all my wordpress pages.

I had it in the wrong place, after much testing I found out if I place this rewrite before the RewriteCond statements, it works, and it doesn't brake other Wordpress rewrites.
So the final is:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^homes-for-sale-details/4420/217007490/177/Fb-landing-page(.*)$ propertylisting1/index.php?success=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Related

Wordpress URL rewrite subfolder to another subfolder

I have been trying for several hours now to do a simple url rewrite on a fresh wordpress install and not having any luck.
Suppose my website has a page with some content
-example.com/samplepage
If someone types in the browser example.com/au/samplepage,
I want to rewrite the URL so it loads example.com/samplepage but keeping example.com/au/samplepage in the browser URL
I have tried ^ax/?(.*)$ /global/$1 [L] in my htaccess but no luck. I feel as though wordpress is over-writing something as it just displays the 404 error page.
I can however do a redirect from example.com/au/samplepage to example.com/samplepage where the browser URL example.com/samplepage, however if I want to keep the original URL, it seems impossible.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^au/?(.*)$ /$1 [NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Any help will be greatly appreciated
I wouldn't think this would need mod_proxy? I have also tried the [PT] flag but no luck.

301 Redirect all page and post urls from .html to /

I have a current site which got all URLS end with .html.
I created a new site and the URLS are pretty much the same but without .html.
I've been trying codes found here in my .htaccess file and most seem to cause Internal Server Error.
http://example.com/page1.html to http://example.com/page1/
http://example.com/page1/page2.html to http://example.com/page1/page2/
My current htacces file code is:
# 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 install Simple 301 Redirect plugin and try to setting like image below. Just tested on my live site and it's running exactly what you want.
Remember you checked Use wildcards? checkbox too.

Redirect url of wordpress site on the basis of string using htaccess

I have a wordpress site eg. http://www.domain.com/ with custom theme hosted on a linux server and working fine. Some marketing strategy requires me to distinguish between 2 kinds of URLs (that they will be shooting from ads) and based on that redirect the site to different links.
eg:
If the URL contains /xyz/ ie:
http://www.domain.com/xyz/category/post I want it to be redirected to
an intermediate page ie: http://www.domain.com/intermediate.php and if
the url doesnt contain /xyz/ ie:
http://www.domain.com/category/post the post should show up as
usual.
I found out that this cant be done inside the wordpress code as before the first hook is triggered, the url is processed and a 404 page is thrown.
The only way I can achieve this is by modifying .htaccess file.
I found a code which reads:
RewriteRule ^(.*)/xyz/(.*)$ http://www.domain.com/intermediate.php [L,R=301]
and second way reads:
RewriteCond %{REQUEST_URI} /xyz/
RewriteRule .* http://www.domain.com/intermediate.php
I am really confused about using it with the existing code in the htaccess file created by wordpress which reads:
# 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 tried merging the codes ie:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /domain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /domain/index.php [L]
RewriteCond %{REQUEST_URI} /xyz/
RewriteRule RewriteRule ^(.*)/xyz/(.*)$ http://domain.com/intermediate.php
</IfModule>
but only one of them work at a time ie: when the redirection works, wordpress posts doesnt show up and vice versa.
Kindly show me a better way.
Immediately after the RewriteEngine On and RewriteBase directives in the "existing" WordPress code try this:
RewriteRule xyz/ http://www.domain.com/intermediate.php [L,R=301]
This will search for "xyz/" anywhere in the requested URL and redirect when found. Note that in .htaccess files, the directory prefix (/ in this case) is removed from the URL path before pattern matching. So, a pattern that starts / will never match at the start of the URL.
External redirects should generally come before internal rewrites, which is what the default WordPress directives do.
The alternative method you mention is less efficient:
RewriteCond %{REQUEST_URI} /xyz/
RewriteRule .* http://www.domain.com/intermediate.php [R=301,L]
This will effectively do the same thing but will result in every request being processed because of the generic .* pattern. Note that the REQUEST_URI does start with a / (directory prefix).

How to use mod_rewrite without clashing with Wordpress

So I have Wordpress installed, let's call the domain test.com. The .htaccess that wordpress created in the default directory (the one that is one level above wp-content) is
# 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 want to add another rule, which if triggered, should case Wordpress to not rewrite. So for instance, suppose I want test.com/nochange to redirect to test.com/script.php, instead of doing the normal Wordpress redirects. Normally the .htaccess for this would be
RewriteRule ^nochange$ ./script.php [NC,L]
But what happens is that Wordpress ends up running anyways, and of course a 404 not found occurs. If I removed the Wordpress .htaccess components, everything works. What do I do to get them to work together?
Ah, I just needed to use the END tag.

.htacess custom redirect rules conflicting with Wordpress redirect rules

I am in the process of moving my current static site to Wordpress, using the same domain name.
Both old and new sites have their content in the root folder and also have identical file names, but on the new (Wordpress) site, I have removed the .php extension that exists on the current site and added a trailing slash, as per Wordpress permalinks.
So I need to redirect all the old pages (for example):
/contact-us.php -> /contact-us/
I have tried the well-documented options such as (and variations on this):
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php$ /$1 [R=301,L]
But this seems to be conflicting with the Wordpress .htaccess rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
When I try to access www.site.com/contact-us.php, I am getting either an infinite loop OR a redirect to www.site.com/index (no .php, no trailing slash), which Wordpress displays as a 404 page.
I have tried with and without the first set of Rewrite conditions (as I see they are duplicated in the Wordpress rules), and also placed my rule before and after the Wordpress rules. No joy. I've also been Googling for the last few hours but no one seems to have addressed this specific problem. I do usually find what I am looking for by searching, so it's in desperation that I'm actually posting (and which is why it's my first post!)
If anyone can help out, I would be very grateful.
It appears you will run into loops when trying an .htaccess redirect because of the php suffix. Maybe someone else knows a solution.
But try a plugin called Redirection « WordPress Plugins. Out of curiosity, I tried a redirect from contact-us.php to contact-us/ and it worked fine. The plugin redirects via php rather than writing to .htaccess, and that may be the difference.
I use the plugin all the time to mostly log 404's and do a few redirects. It doesn't take too much site overhead.
Be sure and set Options to not do "URL Monitoring," as that will set up automatic redirects to to posts/pages that have their permalinks manually changed. And set your 404 logging to a day or two, because the logs can quickly get big and result in huge database tables.
Try using this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Rewrite /something.php to /something/
RewriteRule (.*)/(.*)\.php$ $1/$2/ [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This method would rewrite:
/contact-us/test/hello.php -> /contact-us/test/hello/
If you don't want subdirectory rewriting replace line 6 with:
RewriteRule /(.*)\.php$ /$1/ [R=301,L]

Resources