.htaccess, Wordpress & vanity URLs

2019-07-23 17:00发布

I've been pouring over every Stack Overflow topic I can find on htaccess & vanity urls within Wordpress, but I'm completely stuck as to why mine isn't working. I'm a complete noob with htaccess, so I'm sure that has a lot to do with it.

I am trying to format all urls pointing to /dealers/dealers-info/username to the same wordpress page (id 112 - aka 'dealers-info') with the username as a parameter.

The vanity url code added is right after #vanity urls

For example, passing: URL.com/dealers/dealers-info/watergallery where 'watergallery' is the username, displays a basic 404:

Not Found

The requested URL /dealers/dealers-info/watergallery was not found on this server.

Any insight is greatly appreciated - thanks in advance for your help!

[EDIT - removed leading / and moved the rule - now seeing a WP 404 page]

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin

RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteRule ^uvmax/blog/$ /blog [R=301,L]
RewriteRule ^dealer-finder/$ /dealers [R=301,L]
RewriteRule ^sterilight/blog/$ /blog [R=301,L]

# vanity urls
RewriteRule ^/dealers/dealers-info/(.*)$ index.php?p=112&username=$1 [L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

2条回答
小情绪 Triste *
2楼-- · 2019-07-23 17:36

RewriteRule ^dealers/dealers-info/(.*) index.php?p=112&username=$1 [L]

查看更多
乱世女痞
3楼-- · 2019-07-23 17:42

Thanks everyone for your help and suggestions!

It turns out that WP's built-in RewriteRule functions are what need to be used (at least as far as I can tell).

This solution worked for me, thanks to a coworker (James) who discovered these WP Codex examples!:

http://codex.wordpress.org/Rewrite_API/add_rewrite_tag http://codex.wordpress.org/Rewrite_API/add_rewrite_rule

 add_action('init', 'dealerRewrite');

 function dealerRewrite(){
    add_rewrite_tag('%username%','([^&]+)');
    add_rewrite_rule('^dealers/dealers-info/([^/]*)/?','index.php?page_id=112&username=$matches[1]','top');
 }
查看更多
登录 后发表回答