wordpress custom htaccess rewrite not redirect - wordpress

I am trying to create 'faux' pages that all link to the homepage with associated variables in the GET data. For example:
http://example.com/img/IMGNAME/comment/5678/
provide data like:
http://example.com/?image=IMGNAME&comms=5678
I have the following in my htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^img/([^/]+)/comment/([^/]+) /?image=$1&comms=$2 [L,NS]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
the issue is that i have a post that has a slug of IMGNAME (this is how im finding the post given the GET variable on the homepage).
When I manually enter http://example.com/?image=IMGNAME&comms=5678 it all works great.
When I enter http://example.com/img/IMGNAME/comment/5678/
I am redirected to http://example.com/category/IMGNAME/ (the url address changes AND it renders the single.php template)
I am assuming index.php (or '/') then gets processed by wordpress, in which it attempts to find the post with the closest name to the content of the variable im handing it and REDIRECTS to the post?
Does anyone know how to have the entered url STAY in the address bar and allow the homepage to process the GET variables?
Do i need to create a different version of the base index.php file to have it not redirect/search for similarly named posts? I still need it to process my theme template pages with wordpress tags etc.

I think rather then doing the rewriting in .htaccess you should add new custom rewrite rule wordpress way.
function my_custom_rewrites(){
add_rewrite_rule(
'img/(.*)/comment/(.*)',
'index.php?image=$matches[1]&comms=$matches[2]',
'top' );
}
add_action( 'init', 'my_custom_rewrites' );
You can also try the tips shared here http://docs.dev4press.com/tutorial/practical/debug-wordpress-rewrite-rules-matching/ that might help you understand which Rewrite rule is matching the url and why a particular template is being rendered.
Your URL might very well be matching with other patterns of rewrite rules of Wordpress. So registering your own rule in wordpress gives you the opportunity to get it checked before the core rules, Notice the third parameter of the add_rewrite_rule which says 'top', this specify the same

Related

url masking via htaccess rewrite is not working

Not sure if this is the right section of Stackoverflow to ask my question...
But here it is:
So I am using the below on the .htaccess file:
RewriteEngine On
RewriteRule ^sale?$ /discount-page/
So that when people visit example.com/sale page, they see content from example.com/discount-page/
But when I visit example.com/sale it shows 404 error saying that the URL /discount-page is not available on this server...
Why is it happening?
Here's how my entire .htaccess file looks like:
# 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 please?
When using WordPress, you can't simply rewrite the URL in .htaccess to the %postname% (the real URL) since WP still looks at the REQUEST_URI in order to route the URL. Even though you are rewriting /sale to /discount-page/ (the actual URL), WordPress sees /sale (the requested URL) - which doesn't exist inside WP; hence the 404.
Although not your intention, you could change this to an external redirect to get around this problem (which also avoids a potential duplicate content issue). For example:
RewriteRule ^sale$ /discount-page/ [R,L]
(I removed the ? in ^sale?$, as that does look erroneous. Or do you really want to match /sale or /sal?)
Alternatively, you could try rewriting to the underlying "plain" permalink. ie. explicitly pass the %post_id%. This is different to rewriting to the %postname%, since WP shouldn't need to check the REQUEST_URI in order to route the URL. For example:
RewriteRule ^sale$ /index.php?p=123 [L]
Where 123 is the %post_id% of your discount-page. By rewriting directly to index.php, you are effectively bypassing WP's front-controller.
Note that this must go before the standard WordPress directives in .htaccess (aka the front-controller).
However, I still feel there should be a more WordPress-like way of doing this, which is why I initially suggested asking this question over on the WordPress Stack. However, if you do that, don't mention ".htaccess". What you are really creating is a URL alias or something like that. For example: Have two different URLs show the homepage

How can WordPress and YOURLS peacefully coexist at the root level?

I want to make a better link shortener service using YOURLS. I've procured a short URL for this: sly.pe. YOURLS is installed and sly.pe/admin is accessible.
I'd also like to use WordPress to power most of the consumer facing site, at the URL: sly.pe. The idea being I want users to register for the site using WordPress and a registration plugin like OneAll social. Once authenticated they get logged in for both YOURLS and WordPress and are redirected to a URL like sly.pe/home that uses WordPress as the site but YOURLS (sly.pe/admin for that user) is embedded and accessible somehow.
Both services make .htaccess changes. If I leave the file as is, I can't seem to have any other WordPress URL other than sly.pe, but YOURLS works fine. If I comment out some YOURLS code, WordPress can use other directories, but then YOURLS doesn't work. How can I have both coexist nicely? Or at least define the WordPress URLs I want and get those working?
(commenting out the YOURLS code gets WordPress working, but breaks YOURLS):
.htaccess:
# BEGIN YOURLS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /yourls-loader.php [L]
</IfModule>
# END YOURLS
# 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 was able to get YOURLS to work with WordPress. Here's how I did it...
I've placed YOURLS in niemannross.com/links - so for me, a redirect looks like http://niemannross.com/link/rlunchlessons
Install the Redirection plugin by John Godley in your copy of WordPress.
Look in Wordpress:Admin:Tools:Redirection. Across the top are several tabs. You should see Redirects | Groups | Logs | 404s | Import/Export | Options | Support.
Choose Redirects
Select Add New Redirection
Source URL: /link/.* - Select Regex.
Target URL: /link/yourls-loader.php
Group: Whatever makes sense in your setup. I've chosen wordpress:redirections
This works for my installation.
Simple answer is: you cannot. Both WordPress and YOURLS (please, YOURLS, not YourLS) need to process server requests to return the expected result (eg when requesting http://sho.rt/blah, each app needs to check if blah is content it manages)
Long answer: you should not because all of the following.
Basically you would need something that works when:
blah is an WP article but not a YOURLS short URL
blah is a YOURLS short URL but not an WP article
blah is neither a WP article or a YOURLS short URL
optionally decide what to do when blah is both a WP article and a YOURLS short URL
Your code would also need to :
prevent WP to create a "slug" that is already a YOURLS short URL
prevent YOURLS to create a short URL that already matches a WP slug
You'd need to have code for this both in the WP area and in the YOURLS area.
Bottom line: lots of edge cases to deal with. What might work in some case on your side might not work in other cases, on your side or with others.
I think you're going to have to work around the problem as mentioned by #Ozh, but here's a plugin that will make it easier for your WordPress users to use YOURLS: https://wordpress.org/plugins/yourls-link-creator/
You cannot, I tried different ways, maybe with new apache 2.4 using the if else statement you will, but I am not an expert on those commands, I want to share what I did to have the shortener option in the root page and the html site in a subdirectory, with this solution you can still give people normal url for the website (e.g. sho.rt):
set your website in a folder under root with a name e.g. "w", that means it will be at "sho.rt/w"
then use this .htaccess
# BEGIN YOURLS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /yourls-loader.php [L]
RewriteRule ^$ /w [L]
</IfModule>
# END YOURLS
This will give you the option to still use sho.rt as your website address if the user doesn't pass any subfolder in the url, but if they do, it will check in yourls first and go to your folders if nothing found.
One more thing, do not forget to add "w" in the $yourls_reserved_URL array in /user/config.php (Yourls config file)
I finally solved this for my website.
I have a subdomain http://sub.domain where I installed YOURLS, but the short URLs are at http://domain/someshortlink
And the wordpress website is also at http://domain/
It used to work with the swap plugin ( https://github.com/gerbz/Yourls-Swap-Short-Url ).
Just with the htaccess provided by that YOURLS plugin, and no wordpress. But since I decided to move to wordpress, this combination won't work.
Here is the trick:
On your htaccess, keep it simple, as wordpress wants it to be. Remove the snippet htaccess for the YOURLS swap plugin, if you have it added.
Edit your 404.php page theme. Mine was at: wp-content/themes/twentynine/404.php
At the very begin, just add:
<?php
header('Location: http://sub.domain' . $_SERVER['REQUEST_URI'] );
exit;
?>
This will allow:
All files and directories will honor the basic htaccess from which they are simply served
All URLS not files and folders will be handled by wordpress in the usual manner.
If a page is not found, wordpress will trigger 404.php which will send that page to YOURLS to try
If YOURLS also don't find it, a 404 page from YOURLS will show.
Remember to edit the 404 page from YOURLS also, to match your wordpress theme.
Best of luck!
Reference: https://github.com/gerbz/Yourls-Swap-Short-Url/issues/11

Modify friendly URLs generated by default in Wordpress (via .htaccess)

I have a website done in Wordpress and I need to make some changes in the fiendly URLs.
I’ve created a page from the admin panel named detail, this page reads the template file detail.php from the templates folder.
The URL that is currently mounted is http://www.domain.com/detail/1234/ and I need that it could be accessed as http://www.domain.com/anything/1234/.
The following lines have been generated by Wordpress but I don’t understand them and I don’t know how to modify them for my purpose:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
First you should really understand what those rules are doing and what you really want to achieve. Then you can try to change the system to fit your needs.
IfModule ensures everything inside is processed only when mod_rewrite Apache module is present. All the other directives are from this module.
RewriteEngine On enables URL rewriting.
RewriteBase / tells the engine that the rules operate inside root. See also the general question on how RewriteBase works.
RewriteRule ^index\.php$ - [L] means that no more rules should be processed ([L]) if the current URL to be rewritten is index.php. No rewrite takes place. RewriteRule directive accepts a regex. See also regex tag here on SO.
All RewriteCond directives apply to the following RewriteRule. Unless [OR] flag is added, they must be all satisfied at the same time to execute the rule. In this case they mean:
Requested resource is not a regular file.
Requested resource is not a directory.
Rewrite any (at least one character long) URL to index.php. This is the last ([L]) rule to be processed.
When adding new RewriteRules, you probably want to use the WordPress way of doing this, as described in Zac’s answer. Figuring out the right rule by analogy to the many examples in the manual or here on SO should not be hard.
Put into functions.php maybe a better idea:
functions.php
function setRewriteRule($orgRules){
return array( '/([^/]+)/([0-9]+)/?' => 'index.php?post=$matches[1]' ) + $orgRules;
}
add_filter('rewrite_rules_array', 'setRewriteRule');
Then you just need flush the rewrite rules, I usually use 'rewrite-rules-inspector' plugin.
This should solve your problem give it a try ... you can write your own custom permalink without adding any code also if someone tries to access the page via old URL they will be redirected to the new one.
WordPress Custom Permalinks
After installing this you just have to go into pages and type your own URL below the heading

Redirect Posts to Categories in WordPress

I've been scouring the Internet looking for an answer to this but haven't yet. On a WordPress site, how do I redirect all posts from a particular category to that category page rather than the single post view using the .htaccess file? For example, I have the following category location:
www.example.com/websites/
which will show all posts in the "websites" category. I have a post in this category at the following location:
www.example.com/websites/some-website-example/
When the search results on my site show a link to my Some Website Example post and the user clicks on it, how do I get them to go to the category page instead of the single post view? In other words I want them to always go to,www.example.com/websites/ instead of www.example.com/websites/some-website-example/, or www.example.com/websites/another-example/, or any other single post.
I am very new to modifying the .htaccess file, so what code do I need and where do I put it? Here's what I have:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
# END WordPress
And again, what I want is a rule that redirects all requests for pages under the /websites/ directory to the /websites/ directory itself.
p.s. For those of you who need a reason for why I would do this, I'm using the site as a portfolio and would like users to see all my website with their descriptions and other post information together on the page all at once, and never want them to see a single website on the page.
The solution is to set a 301 redirection on .htaccess.
redirect 301 /old/old.htm http://www.you.com/new.htm
However, I don't think it is a good idea. If I would need to do such a thing, I'd add to my WP instance non public post-type for example "websites items". Then you should add a page named "websites". If you have a page, you can make a custom query on page-websites.php template file.
That should work and your standard Wordpress RSS shoudn't have te "webistes/website1" sites, so Google won't go there anyway.
Here is the CODEX site with all possible information about registering post-types. It's really nice feature provided since WP 3.0.

Wordpress stop post redirects

I'm having a Wordpress problem regarding permalinks.
When I have a post whose permalink is say, /2009/10/podcasts, trying to access /podcasts redirects to /2009/10/podcasts. Is there any way to stop this behavior so I can handle it as a 404?
I'm using a custom 404 handler that checks if the request is a 404 error and executes a Kohana request from within Wordpress.
I just got the answer on the WP forums. It's
remove_filter('template_redirect', 'redirect_canonical');
just checked with a default installation of wordpress - and indeed it seems to be a default behaviour of wordpress to look for the path anyways - no matter what directories you add hide. So also replacing the "/2010/01/some-long-url" by "/error/some-long-url" will redirect the reuest to "/2010/01/some-long-url".
Anyways - I can still suggest two workarounds:
1) if you really want to get a real 404 error you can use the redirect method in your htaccess to forward the request to a non-existing url - just add the one redirect-line like this:
Redirect /podcasts /podcasts-error
all together the htacces could then look like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Redirect /podcasts /podcasts-error
</IfModule>
2) If you don't really need the 404 error you may also just add a page and give it the exact same url - in this case it would be "podcasts". That would de-activate the forwarding. You could then add your own custom 'error message' to that page..
Greetz, t..

Resources