问题
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