How to format URL on Website with htaccess

人盡茶涼 提交于 2020-01-07 09:21:42

问题


I would like some help in formatting a url so that it is more SEO friendly. So the url is formatted below

www.portfolio.com/workDetails?url=work-title-here

but I would like the format to be

www.portfolio.com/work/work-title-here

I've tried the following

RewriteEngine on
RewriteRule ^/work/([0-9]+)\.html /workDetail.php?url=$1

But I got a 500x error


回答1:


This page appears to have a solution to what you are trying to do:

<?
    $Params = explode( '/', $PATH_INFO );

    while( list( $Index, $Value ) = each( $Params )) {
        echo "Params[ $Index ] = $Value<BR>\n" );
    }
?>

Hit this URL:

http://whatever.site.com/ThisIsAProgram/these/directories/are/not/real

You should get:

Params[ 0 ] =

Params[ 1 ] = these

Params[ 2 ] = directories

Params[ 3 ] = are

Params[ 4 ] = not

Params[ 5 ] = real

And to display the PHP file as just the file name, try this:

Options +MultiViews
RewriteEngine On

RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]

EDIT: If you are wanting a more complete example, this answer covers it a lot more in depth than I could. This will definitely help you cover all your bases and understand the issue if my other linked article doesn't make it clear enough.



来源:https://stackoverflow.com/questions/57666758/how-to-format-url-on-website-with-htaccess

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