简体   繁体   中英

How to use preg_replace and htaccess to modify url?

I modified it according to your suggestion

<a class="upheader" href="leagues/' . $rs['ext'] . '/' . $rs['uniqueid'] . '" target="_blank">' . $rs['title'] . '</a>

now, I get /leagues/pl/xxxxxx as the new url

however, it does not find the page, displaying File not found (404 error)

This is my entire .htaccess file

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule leagues/pl/([0-9]+)/?$ pl?uniqueid=$1 [L]

In my previous example I used 'pl' instead of $rs['ext'] to make it easier to explain. Also, I don't know how to use variables in .htaccess

I would immensely appreciate if you could show me how to use preg_replace or any other relevant function so that my code would look and work something like

<a class="upheader" href="leagues/' . $rs['ext'] . '/' . preg_replace('/\s\s+/', $rs['uniqueid']) . '" target="_blank">' . $rs['title'] . '</a>

and the url display something like

/leagues/pl/xxxxxx

pl being the variable

with the help of .htaccess

Using my .htaccess as an example:

# Rewrites
RewriteEngine           On

# every html-element that uses a src="" needs these:
RewriteCond             %{REQUEST_URI}          ^/styles/.*$
RewriteRule             ^styles\/(.*)           /sites/%{SERVER_NAME}/styles/$1         [L]

RewriteCond             %{REQUEST_URI}          ^/includes/.*$
RewriteRule             ^includes\/(.*)         /sites/%{SERVER_NAME}/includes/$1       [L]

RewriteCond             %{REQUEST_URI}          ^/images/.*$
RewriteRule             ^images\/(.*)           /sites/%{SERVER_NAME}/images/$1         [L]

# everything else will be directed to the requesthandler:
RewriteCond             %{REQUEST_URI}          !^/core/libraries/FCKeditor/.*$
RewriteCond             %{REQUEST_URI}          !/cache/.*$
RewriteCond             %{REQUEST_URI}          !/images/.*$
RewriteCond             %{REQUEST_URI}          !/styles/.*$
RewriteCond             %{REQUEST_URI}          !/includes/.*$
RewriteCond             %{REQUEST_URI}          !^/requesthandler.php
RewriteRule             (.*)                    /requesthandler.php                     [L]

The requesthandler.php uses the $_SERVER['REQUEST_URI'] to retrieve the requested data from either database or filesystem.

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