URL redirection in Wordpress manually by editing .htaccess file

隐身守侯 提交于 2019-12-25 02:31:18

问题


please help me to fix my one of wordpress problem with URL redirecting in PHP.

This is my original wordpress htaccess file.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

I want to create custom URL redirection here. For ex., I want to redirect pages,

http://example.com/b/1    >     http://example.com/b?n=1
http://example.com/b/2    >     http://example.com/b?n=2

i am using directory URL type (displaying URLs as directories without extensions) SEO options in wordpress settings.

inside of http://example.com/b page, I have included another php file using ‘include(‘z.php’);’ command. Z.php is reading URL parameter comes from ‘/?n=1’ through redirected URL. I tried below solution, but no luck..

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^b/([^/]*)$ /b/?n=$1 

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Appreciate any of help.

EDIT

wordpress header file can read the translated parameters. i checked that by displaying GET parameter 'n' in title. but it shows file not found for http://example.com/b/

i verified that, by editing http://example.com/b/ page content. non of page content displaying after rewriting.

Any help appreciated

EDIT

URL may have characters in parameter as below.

http://example.com/b/abc_1    >     http://example.com/b?n=abc_1
http://example.com/b/aa_2    >     http://example.com/b?n=aa_2

Thanks for Helping


回答1:


At first sight it seems to me that it should be

RewriteRule ^b/([^/]*)$ /?p=$1

Wordpress doesn't know what to do with the url /b?n=X.




回答2:


If you want http://example.com/b/abc_1 to work, the abc_1 part must match the slug of a wordpress post.

Additionally you have to change the settings->permalink structure to match either postname or a custom structure like /b/%postname%.




回答3:


http://example.com/b/1

to

http://example.com/b?n=1

Using this

RewriteRule ^b/([0-9]+)/?$ b?n=$1 [NC,L]


<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^b/([0-9]+)/?$ b?n=$1 [NC,L]
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

Test hear

More info




回答4:


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^b/([^/]*)$ /b/?n=$1 

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#add below line
RewriteRule ^b/([0-9]+)/$ http://example.com/b?n=$1 [L]
</IfModule>    



回答5:


I found the word press related solution for this question.

in word press all the requests are heading to index.php automatically. if we create custom rewrite to another page, that request not going through index.php. that makes wordpress unidentified request and wordpress will return page not found message.

becasue of that, we have to redirect custom rewrite to index.php instead of permalink.

i used

RewriteRule ^b/([^/]*)$ index.php?page_id=4&n=$1

this rule redirected all requests matched with rule to index page. (my page ID for /b/ is 4) this worked successfully for me.

other solutions above worked for non wordpress situations. but i found wordpress behavior as i explained.

appreciates who tried to answer.



来源:https://stackoverflow.com/questions/23700518/url-redirection-in-wordpress-manually-by-editing-htaccess-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!