How to rewrite url in htaccess to make user freindly url - php

I have a site in wordpress in which a page is showing questions based on its ID.
http://example.com/index.php/question/?questionid=9&title='how-to-rewite-htaccess'
I am the beginner to php I'm not able to graps other examples found on stackoverflow.com
Here's my .htaccess code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^/question/([^/]*)/([^/]*)/$ /question/?questionid=$1&title=$2 [NC,L]
</IfModule>
But this is not working. please tell where is error.

Since your using Wordpress you can turn on permalinks which takes care of the url friendly urls for you see https://codex.wordpress.org/Settings_Permalinks_Screen.
Login to your admin then go to permalinks and set a preferred style.

example.com/index.php/question/9/how-to-rewite-htaccess doesn't match your rewrite rule because the rule has a trailing /. Change the regex (first) part of your rewrite rule to:
^/question/([^/]*)/([^/]*)/?$
Adding ? makes the trailing / optional.

Once you go to setting > permalinks and set the pretty links, WordPress will automatically generate .htaccess based on the setting you choose. Don't forget to save.
More information here http://www.wpbeginner.com/beginners-guide/why-you-cant-find-htaccess-file-on-your-wordpress-site/

Related

.htaccess not working Wordpress. Make a url go to a file

I am trying to make a URL go to a file which is a WordPress page template.
I am doing a website that has categories and subcategories then a details page.
First off I am try to make.
www.example.com/explore/category_name
The category_name is dynamic and pulled from a non WordPress database.
I am trying to get that any time the URL is www.example.com/explore/whatever. It points to /wp-content/themes/my_theme/myfile.php
Here is my .htaccess file.
RewriteEngine On
RewriteBase /
RewriteRule ^explore/([a-z0-9-]+)$ /wp-
content/themes/visitsi_theme/listing-page.php [NC]
#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
RewriteRule ^explore/([a-z0-9-]+)$ /wp-
content/themes/visitsi_theme/listing-page.php [NC]
You need to include the L flag to prevent the WP directives that follow being processed. I would also remove NC flag and make the "whatever" match A-Z if required. Otherwise, /ExPlOrE/ would be accepted (rather than generating a 404). This directive is also split across two lines (in the middle of the substitution) - although maybe that's a typo? (But it certainly needs fixing.)
So, try the following instead:
RewriteRule ^explore/([a-zA-Z0-9-]+)$ /wp-content/themes/visitsi_theme/listing-page.php [L]
visitsi also looks like a typo - but maybe that's intentional?
Providing /wp-content/themes/visitsi_theme/listing-page.php is a physical file, then the WP front controller should ignore it.

mod_rewrite in Wordpress installation to replace multiple keys/variables

I am trying to create mod_rewrite rules to redirect the following url in my wordpress site:
www.mywebsite.com/pg/2/row/20/filter/all/filtercity/foo,bar
to
www.mywebsite.com/detail?pg=2&row=20&filter=all&filtercity=foo,bar
My problem is that wordpress included the following rules in 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
I tried the following RewriteRule and it kind of works if I comment the last RewrireRule from Wordpress.
# 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]]
RewriteRule ^pg/([a-zA-Z0-9]+)/?$ ?pg=$2 [R]
</IfModule>
# END WordPress
The above works only for urls like
www.mywebsite.com/pg/2 => www.mywebsite.com?pg=2
A few questions:
1 - how can I configure .htaccess to process the url only when it finds the following keys
/pg and/or /row and/or /filter and/or /filtercity
for all other urls, it should execute the standard Wordpress RewriteRule . /index.php
2 - also the url can have all of the keys or only a few of them. For example:
/pg/2/row/20
/row/20/filter/all
how can I configure the .htaccess to process all the different combinations of keys?
3 - when I tried replacing
RewriteRule ^pg/([a-zA-Z0-9]+)/?$ ?pg=$2 [R]
with
RewriteRule ^pg/([a-zA-Z0-9]+)/?$ detail?pg=$1 [R]
I got a 404 error from the server. Not sure why. Any ideas?
Thank you.
EDIT 1:
I tried adding [L] to the end of my RewriteRule (see below)
RewriteRule ^pg/([a-zA-Z0-9]+)/?$ ?pg=$1 [R,L]
and moving the standard Wordpress rewrite rule to the next line
RewriteRule . /index.php [L]
After doing that I no longer get server 404 error. However the CSS files stop loading.
1 - how can I configure .htaccess to process the url only when it
finds the following keys
/pg and/or /row and/or /filter and/or /filtercity
Read this Rewrite URL with .htaccess for multiple parameters
The above answer helps you to solve your problem when have a standard url format.
2 - also the url can have all of the keys or only a few of them. For
example:
/pg/2/row/20
/row/20/filter/all
You are trying to do something out of URL semantics. When you try to access a resource, the path should be same every time. You can pass pg/0/ if you are on the starting page. But who can stop you from going out of URL semantics? :) In that case you have to have all the combinations as rules in your .htaccess. Follow the above solution, with all URL combinations it will work.
My Suggestions:
Don't Struggle with .htaccess if you are not familiar and your need is only URL rewrite (it is powerful though)
Pass everything to the index file and parse in your code, for your case have a look at this page http://codex.wordpress.org/Query_Overview

Htaccess proxy redirect wordpress error

I want to redirect domainA.com/folder/ which originates within an older wordpress installtion to domainB.com/folder1/folder2/ which is the link to the page within my new wordpress installation wihtout the url changing.
I have managed to achieve this with the .htaccess file inside domainA.com/folder/ which reads:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder
RewriteRule ^(.*)$ http://www.domainB.com/folder1/folder2/ [P]
The Rewriterule [P] (proxy) flag does what I want and redirects the page without changing the url but some of the content within my Enfold WP template such as the advanced layer slider and icons fail to load.
I assume this error is due to some sort of conflict with WP's native htaccess code and I also suspect it has to do with the lines:
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I have experimented with adding the first, third and fourth lines to my code but it doesnt work. Does anybody know how I could resolve this issue?
If it makes any difference the wordpress site I want to direct to (domainB.com/folder1/folder2/) is one whithin a Wordpress Network.
For the moment I have changed the [P] flag to [L] flag which redirects and works but changes the url.
Any help or advice would be much appreciated. Thank you!
Try to change the paths of images (the URLs) in advanced layer slider and icons URLs from
relative to absolute ones (as described in RFC 2396), then you can use the [P] flag in .htaccess again.
Often plugins uses relative paths when outputting content, but this can cause such type of troubles.
This following code takes care of permalinks in WorPress and it is recommended to keep the beginning and ending WordPress lines so that WordPress is able to locate it and modify it as needed. You may already have all this and are just not displaying it. I do not know.
# 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
In the other section you probably need to remove the trailing / after folder2 like so:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder
RewriteRule ^(.*)$ http://www.domainB.com/folder1/folder2 [P]

htaccess file not working for wordpress pages

I'm trying to add a new rule in the htaccess file that will allow input of other instead of the actual name of the page which is notify. But I can't seem to get it to work.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^other$ notify [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Also tried something like:
RewriteRule ^other$ index.php/notify [QSA]
and this:
RewriteRule ^other$ /index.php/notify [QSA]
Also tried adding the rules from wordpress itself (inside the admin_init section of a plugin) and then refresh the rules by going to the permalinks settings, choosing post name and then save but no luck either. Upon checking htaccess file it produced a rule similar to the above one:
add_rewrite_rule('other', 'index.php/notify', 'bottom');
I've verified that I can override the rules via the htaccess file in the root directory because when I tried to use an actual file in the server hello.php
RewriteRule ^other$ hello.php [QSA]
It works. I just don't know why when I use a wordpress page it doesn't work. It also seems to work when I use the absolute path:
RewriteRule ^other$ http://somesite.com/notify [QSA]
But this is no good because when I add some arguments to the URL (which is what I will ultimately do) I end up with bad looking URL when I do the above: http://somesite.com/?some_argument=some_value
The current permalink setting in wordpress is set to post name. Any ideas what I have missed?
Try this rule:
RewriteRule ^other/?$ index.php?p=PAGE_ID [QSA]
Put the necessary PAGE_ID to redirect to
Hello may be you can use .htaccess plugin..! to control it from admin panel. I hope this Help you
http://wordpress.org/plugins/wp-htaccess-control/

htaccess html > php redirect + Wordpress rewrite permalinks structure

I have started a site up using html only, switched it over to php to make my life easier and have used the following code in the .htaccess to ensure all users (who may have bookmarked) are still seeing the right pages:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.+)\.html$ http://domain.com/$1.php [R,NC]
Now I am working on the news section a wordpress blog (inside a /news directory) that would be lovely to use permalinks with, when I slap the rewrite code below into my .htaccess all my files outside of the wordpress directory default to wordpresses 404, they still have the correct address in the bar, but no information.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /news/index.php [L]
</IfModule>
Can anyone help on the correct structure for the .htaccess - I tend to use snippets for specific tasks rather than write htaccess stuff.
Thanks
Your rules have to be come before the Wordpress rules and you need to add an [L] to your RewriteRule:
RewriteRule ^(.+)\.html$ http://domain.com/$1.php [R,NC,L]
Which will tell .htaccess to stop processing rules.
I would recommend a permanent (301) redirect so Google and the other search engines will update their listings:
RewriteRule ^(.+)\.html$ http://domain.com/$1.php [R=301,NC,L]

Categories