htaccess - Removing last segment after unique ID number in url

一笑奈何 提交于 2019-12-25 09:05:01

问题


In case there is something after a string that looks like this "/UniqueID (numbers only)/", e.g.: "/455/", in my url, I want to delete it.

Here is what I have in my htaccess file right now:

RewriteCond %{REQUEST_URI} m.*
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m(.*)$ articles.php?$1 [T=application/x-httpd-php,L]

(I guess it adds a hidden extention "articles.php".

So, if my url looks like this:

www.mondomaine.com/m/234/ - no change needed for the visible part

If it looks like this:

www.mondomaine.com/m/121/some-title/ - I want to delete "some-title/"
www.mondomaine.com/m/121/ - result expected for the visible part

So, I guess we have to find "/numberonly/" and delete everything after it, then make sure the "articles.php" part remains. But I do not know how to do it.


回答1:


(I guess it adds a hidden extention "articles.php".

No.

The RewriteRule takes an address that starts with m (and then anything) and gives it to articles.php page.

So:

Input (what browser sees):

m.domain/m/cheese/goats

Output (page server actually loads):

m.domains/articles.php?/cheese/goats

And from my answer to your previous question, which does work:

 RewriteEngine On
 RewriteRule ^(m/[0-9]+)/(.*)/?$ [NC,L,QSA]

This will rewrite any url beginning with m/(numbers) to cut off anything after the numbers.

So to use this in your current .htaccess:

 RewriteEngine On
 RewriteCond %{REQUEST_URI} m.* #optional. 
 RewriteRule ^(m/[0-9]+)/?(.*)$ /$1 [NC,L,QSA]

 RewriteCond %{REQUEST_URI} m.*
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^m(.*)$ articles.php?$1 [T=application/x-httpd-php,L]

So now: input = http://www.mondomaine.com/m/121/treesand-horses/

result of FIRST PART ONLY: (visible in the browser as a redirect):
http://www.mondomaine.com/m/121

And SECOND PART (not seen by browser):

http://www.mondomaine.com/articles.php?/121



来源:https://stackoverflow.com/questions/39292518/htaccess-removing-last-segment-after-unique-id-number-in-url

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