URL rewriting doesn't work - wordpress

There are some pages in this format:
http://www.saglikkosesi.net/soru-ve-yanitlari-goster?page=2
I want them to appear in this format:
http://www.saglikkosesi.net/soru-ve-yanitlari-goster/page/2
I used the following code in .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule soru-ve-yanitlari-goster/page/(.*)/ soru-ve-yanitlari-goster?page=$1
RewriteRule soru-ve-yanitlari-goster/page/(.*) soru-ve-yanitlari-goster?page=$1
Nothing changed. What can I do to fix this? (I use WordPress platform for my web site. But I can't use permalink feature for this redirection as this pages are external pages and I can't control them via WP admin.)

Are you sure that .htaccess files are enabled by your Apache configuration?
By the way, you do that by setting AllowOverride All in the <Directory section of your httpd.conf file.

You need RewriteCond to access the query string and capture the relevant part:
RewriteCond %{QUERY_STRING} ^page=(\d+)
RewriteRule ^/soru-ve-yanitlari-goster$ soru-ve-yanitlari-goster/page/%1?

Related

Apache rewrite to map # based URL into proper URLs

Long version (you can skip to TL;DR if you want to):
I am working with a Wordpress site that was set up by someone else. The website has multiple pages where page has tabbed content which is accessible through #. For eg:
www.example.com/services/category1/#tab-service1
www.example.com/services/category1/#tab-service2
www.example.com/services/category2/#tab-service1
www.example.com/services/category2/#tab-service2
www.example.com/services/category2/#tab-service3
Now, when search engines index they are indexing only www.example.com/services/category1/ and www.example.com/services/category2/. This creates a problem where we cannot have search engines point directly to the content within a given tab. What we want is for search engines to show links that takes users directly to (say) www.example.com/services/category2/#tab-service3.
Now, I don't think google can index such # based content on its own. So, I am thinking of using apache rewrites to try to resolve this issue. I have access to .htaccess file only (from a config perspective).
TL;DR
How to redirect www.example.com/services/category1/service3/ to www.example.com/services/category1/#tab-service3 using apache redirects (I have access to .htaccess file)?
This is what I am trying but it's not working:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/services/category1/([a-z0-9])/? [NC]
RewriteRule .* /services/category1#tab-%1 [R,NE,L]
Someone also adviced to look into pushState server config to fix this. I am not sure how to use pushState.
UPDATE:
I have updated the rewrites to the following but it still doesn't work. It keeps showing Wordpress' 404 page
<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /domainfolder/
RewriteCond %{REQUEST_URI} ^/services/category1/([a-z0-9]+)/?$ [NC]
RewriteRule ^/services/category1/([a-z0-9]+)/?$ /services/category1/#$1 [NE,R,L]
</IfModule>
Your %{REQUEST_URI} regex is wrong. The pattern ^/services/
category1/([a-z0- 9 ])/? matches /services/category1/{any 1 char of a-z or 0-9} format followed by an optional slash. So this does not match your request /services/category1/service3 but matches /services/category1/a/ .
You should be using
^/services/category1/([a-z0-9]+)/?$

postname giving URL was not found

I have an issue by getting permalink to work on debian8.
My .htaccess looks like this:
# 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
and permission 666.
But then I change my permalink to postname instead of plain and I get:
requested URL was not found on this server
on all of my URLs except the start page.
WordPress uses Apache's "mod_rewrite" to serve pages and posts using permalinks, and it sounds like that module is not enabled on your server. To enable it, type sudo a2enmod rewrite, and then restart Apache. After that, permalinks should work.
You can also set any file permissions that you changed back to what they were originally.
Edit:
In your Apache config, you might also need to change the AllowOverride setting. The default on Ubuntu does not allow all .htaccess directives to work, so it's likely the same on Debian. You can use AllowOverride All or choose specific options instead. FileInfo is the one you will need for mod_rewrite. Apache's docs (2.4) for AllowOverride are here: https://httpd.apache.org/docs/2.4/mod/core.html#allowoverride
I solved my problem by using the below command
sudo a2enmod rewrite

Convert .html to -html in file name

I converted my website to WordPress. In the process, in order to satisfy WordPress's rules about permalink formats, I had to change the filenames from ending in .html to ending in -html.
Unfortunately, there are backlinks from other sites that point to the old pages that end in .html. So when someone clicks one of those backlinks, the page cannot be found on the new site.
For this reason, I want to permanently redirect all requests for URIs ending in .html using .htaccess so that they end instead with -html.
So for instance:
https://example.com/file1.html
needs to permanently redirect to
https://example.com/file1-html
Sadly, my code generates 500 errors.
Here's what I wrote based upon what I found in the Apache manual:
<Directory /home/accountname/public_html>
RewriteEngine on
RewriteBase /home/accountname/public_html
RewriteCond $1.html !-f
RewriteRule ^(.*).html$ $1-html [R=301,L]
</Directory>
Can someone help?
<Directory> directive isn't allowed in .htaccess.
Place this rule in your main WP .htaccess:
RewriteEngine on
RewriteRule ^(.+?)\.html$ /$1-html [R=301,L,NE,NC]
Make sure this is first rule below RewriteEngine on line.

Set up CakePHP in a subdirectory; Wordpress is installed in the root

I have been searching for a solution for 2 hours but nothing seems to work... here is my problem:
I have WordPress installed in the root (var/www). So by going to http://www.geekderek.com, I see my wordpress site.
I put CakePHP in a subdirectory var/www/cakephp. I want to be able to see my CakePHP app by going to: www.geekderek.com/cakephp.
However, currently this url just returns a Wordpress page saying "Content not found."
I believe this problem can be solved by modifying .htaccess in my root directory. So here is my .htaccess: http://pastebin.com/sXJTRstB
As you can see, I added this line to the default WP .htaccess file:
RewriteRule ^cakephp(/(.*))?$ cakephp/app/webroot/$1 [QSA,L]
However, for some reason this doesn't seem to work.
Could anyone please tell me what is wrong?? Thank you so much!
I have simmilar setup, that my cake app is in subdirectory, my root .htaccess has this rewrite rule:
RewriteRule ^cakephp/(.*)$ /cakephp/$1 [L,QSA]
All the rest is handled with the regular cakephp setup.
my /cakephp/app/webroot/.htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
hope this works for you too :)
thanat's answer almost worked for me. i just had to edit the RewriteBase to read
RewriteBase /cakephp
and make sure apache conf allowed my root directory to override its previous options
<Directory "/my/root/dir">
AllowOverride All
</Directory>
but then again, my app dir lies outside the DocumentRoot set by apache. and the cakephp dir is actually the webroot itself. so my dir structure is as follows
/my/root
/my/root/app #cake's app dir without webroot
/my/root/dir #apache DocumentRoot (your wordpress is here)
/my/root/dir/cakephp #cake's webroot
also make sure to edit /my/root/dir/cakephp/index.php so it knows where the app and cake's library dirs are.
This is my case, with laravel but they are the same.
https://community.bitnami.com/t/laravel-application-in-subdirectory-of-wordpress-website/79535/2
Just need to change
AllowOverride None => AllowOverride All

Using .htaccess to redirect a domain to another URL

I have a site running wordpress, it's the full site. One of the pages is like a contact-us form located at www.ourdomain.com/contact-us/
I also have a URL like contactourdomain.com and I want it to redirect to www.ourdomain.com/contact-us/
We used to do this with a redirect on network solutions, but I prefer to have it all done right on the server if possible. I've got it sort of working but when you visit the link is still says contactourdomain.com/contact-us/ as the URL, and that breaks all the other ones.
Any suggestions?
.htaccess
Options -MultiViews
RewriteEngine on
RewriteBase /
# Rewrite
RewriteRule ^(.*)$ http://www.ourdomain.com/contact-us//$1 [L]
if you add this in .htaccess is it working?:
RewriteRule ^$ contact-us/ [R=301,L]
keep me posted..

Resources