简体   繁体   中英

.htaccess: Hide URL variables

I have no idea how to play with .htaccess file to change / hide variables from URL

I want to use .htaccess file of WordPress my .htaccess file already contains this scripts:

    # BEGIN WordPress

    <IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /my_site/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /my_site/index.php 

    [L]
    </IfModule>

    # END WordPress

I have url

http://localhost/my_site/project-detail/?pro_id=7

and I want to either hide pro_id=7 like

http://localhost/my_site/project-detail/

but could be able to catch this $_GET['pro_id'] in my php script file.

or

http://localhost/my_site/project-detail/7

I also tried

RewriteRule ^project-detail/(.*) /project-detail/?pro_id=$1

Have you tried something like that:

RewriteEngine On 
RewriteRule ^project-detail/([0-9]+)$ project-detail/?pro_id=$1

You could try adding these above the RewriteBase /my_site/ directive, as they need to be before your wordpress stuff.

First, you can access the actual request directly to redirect to the URLs without the query string:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /my_site/project-detail/?\?pro_id=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /my_site/project-detail/%1?%2 [R=301,L]

This will make it so a URL like this: http://localhost/my_site/project-detail/?pro_id=7 gets redirected to http://localhost/my_site/project-detail/7 and a URL like http://localhost/my_site/project-detail/?pro_id=7&other=param gets redirected to http://localhost/my_site/project-detail/7?other=param . If you don't want any extra params at all, remove the %2 from the rule's target. This makes it so the browser's address bar changes to the URL without the pro_id query string. Now we have to internally rewrite it back to the query string:

RewriteRule ^my_site/project-detail/([^/]+)$ /my_site/project-detail/?pro_id=$1 [QSA,L]

So internally, the URI gets rewritten back to /my_site/project-detail/?pro_id=7 but the browser's address bar still has the clean looking URL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM