web-dev-qa-db-ja.com

カスタム書き換え規則

URLの書き換えをする必要がありますが、これはまったく新しいことであり、いくつか試してみましたが、うまくいかないようです。例:私はこのように見えるURLがあります。

http://example.com/directorio/?filter=all&search_text=&type=medicos&location=quito&cs_directory_search_location=Yes&search_view=&goe_location_enable=No&cs_loc_max_input=5&cs_loc_incr_step=1&submit=&action=cs_directory_map_search

そして私はそれが以下のように見えるようにしたいです。

http://example.com/directorio/medicos/quito/

このようなことを私の.htaccessに直接試しました。

RewriteRule ^directorio/([a-z]+)/([a-z]+)/?$ index.php?pagename=directorio&filter=all&search_text=&type=$1&location=$2&cs_directory_search_location=Yes&search_view=&goe_location_enable=No&cs_loc_max_input=5&cs_loc_incr_step=1&submit=&action=cs_directory_map_search [L]

それはうまくいきません。また試してみました...

add_action('init', 'my_custom_rewrites' );
function my_custom_rewrites(){

    add_rewrite_tag( '%filter%', '([^/]+)' );
    add_rewrite_tag( '%search_text%', '([^/]+)' );
    add_rewrite_tag( '%type%', '([^/]+)' );
    add_rewrite_tag( '%location%', '([^/]+)' );
    add_rewrite_tag( '%cs_directory_search_location%', '([^/]+)' );
    add_rewrite_tag( '%search_view%', '([^/]+)' );
    add_rewrite_tag( '%goe_location_enable%', '([^/]+)' );
    add_rewrite_tag( '%cs_loc_max_input%', '([^/]+)' );
    add_rewrite_tag( '%cs_loc_incr_step%', '([^/]+)' );
    add_rewrite_tag( '%submit%', '([^/]+)' );
    add_rewrite_tag( '%action%', '([^/]+)' );

    add_rewrite_rule(
        'directorio/([a-z]+)/([a-z]+)/?$',
        'index.php?pagename=directorio&filter=all&search_text=&type=$matches[1]&location=$matches[2]&cs_directory_search_location=Yes&search_view=&goe_location_enable=No&cs_loc_max_input=5&cs_loc_incr_step=1&submit=&action=cs_directory_map_search',
        'top' );
}

クエリ変数についても少し読みましたが、前にも述べたように、これは購入したテーマであり、クエリ文字列内のすべての変数に$_GET..ではなくget_query_varを使用してアクセスします。

私はそこで何が問題なのかわからないが、私は考えから出ている、それはRewriteRulesを学び始めるための難しい方法です。誰かが助けることができることを願っています。

こんにちは@Milo、あなたの回答に感謝します、もう1つのこと、アップロードする前に変更をテストするために私のサイトは私のサイトにも同じサイトにもあります。問題は、私のローカルホストで、.htaccessを直接編集してこの規則を使用したことです。

RewriteRule ^directorio/([a-z]+)/([a-z]+)/?$ index.php/directorio/?filter=all&search_text=&type=$1&location=$2&cs_directory_search_location=Yes&search_view=&goe_location_enable=No&cs_loc_max_input=5&cs_loc_incr_step=1&submit=&action=cs_directory_map_search [L]

そしてそのルールは、その醜いURLでも完璧に機能しますが、私がその同じルールをライブサイトに置いたとしても、まったく機能しません。また、そのライブサイトで同じルールを使用しても、絶対パスを指定しても問題なくリダイレ​​クトされる場合は、Rewriteを実行させないでください。私のホスティングがその問題を引き起こしているApacheサーバ上の何らかの設定を持っていることは可能ですか?

2
Dysan G.

.htaccessの間の<IfModule mod_rewrite.c>に以下を追加してください。

RewriteCond %{QUERY_STRING} (^|&)filter=all&search_text=&type=(.*)&location=(.*)&cs_directory_search_location=Yes&search_view=&goe_location_enable=No&cs_loc_max_input=5&cs_loc_incr_step=1&submit=&action=cs_directory_map_search
RewriteRule ^(.*)$ /directorio/%2/%3/? [R=301,L]

これで論理を確認できます .htaccessテスター 。 WordPressから作成されたデフォルトの.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]
# Custom RewriteRule
RewriteCond %{QUERY_STRING} (^|&)filter=all&search_text=&type=(.*)&location=(.*)&cs_directory_search_location=Yes&search_view=&goe_location_enable=No&cs_loc_max_input=5&cs_loc_incr_step=1&submit=&action=cs_directory_map_search
RewriteRule ^(.*)$ /directorio/%2/%3/? [R=301,L]
</IfModule>
# END WordPress