3

Good Morning,

I have documents within the structure of my website, which are accessed with the following url format:

http://example.com/docs/files/123/mydoc.pdf

http://example.com/docs/files/475/otherdoc.pdf

I want when a url of the above type is accessed, it is renamed with the following format:

http://example.com/123/mydoc

http://example.com/475/otherdoc

I have the following rules in the htaccess

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^/docs/files/([0-9]+)/([a-zA-Z0-9-]+)\.([a-zA-Z-]+) /$1/$2 [QSA,L]
</IfModule>

If I type in the browser

http://example.com/docs/files/123/mydoc.pdf

It doesn't change the url to

http://example.com/123/mydoc

What am I doing wrong?

Thanks

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Carl Rguz
  • 27
  • 3
  • Have you turned on the debugging in apache for the rewrite? It will tell you what EXCATLY Apache does during the whole process. Please see: https://stackoverflow.com/questions/9632852/how-to-debug-apache-mod-rewrite – Alexander Dobernig Jan 20 '21 at 12:56
  • "It doesn't change the url to " I think you misunderstand mod_rewrite - IT DOES NOT CHANGE THE URL in the adress line - if this is what you expect. I calls just a completely different file. so if you type www.example.com/test/sausage into the broweser and it has a correct rewrite he will call https:/www.example.com/index.php?action=sausage if you want the displayed URL in the address bar to be rewritten thats a complete different story. - there are a different ways to do it. – Alexander Dobernig Jan 20 '21 at 13:00
  • @AlexanderDobernig mod_rewrite can easily do the other thing as well, if you force an external redirect. That is not really a “completely different story”, main aspect is using the [`[R]`](https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_r) flag. – CBroe Jan 21 '21 at 07:44
  • @CBroe Sorry I did not know this. But on most SEO cases it is not wanted to show the url index.php?action=delete&id=4711 - or do i misanderstand something? I cannot image a use case for it? :-/ and you cannot even use it for people who go to the real url to show the pretty because then you would have never ending circle rewrite?? – Alexander Dobernig Jan 21 '21 at 08:20
  • @AlexanderDobernig of course it makes little sense for `index.php?action=…`, but the original question here was about `/docs/files/123/mydoc.pdf` vs `/123/mydoc`, and in such a case where one wants shorter URLs or something, it might makes sense to external redirect from the longer version, should that be requested directly, to the shorter one, for aesthetic reasons. – CBroe Jan 21 '21 at 08:25
  • _“and you cannot even use it for people who go to the real url to show the pretty because then you would have never ending circle rewrite?”_ – the real URL would _externally_ redirect to the pretty one first, and then when that pretty one get requested, it _internally_ rewrites to the real one again. Those two cases can be distinguished with the right conditions. – CBroe Jan 21 '21 at 08:26
  • @CBroe I needed exactly this some time ago and the question was just downmarked. Do you have any reference? – Alexander Dobernig Jan 21 '21 at 08:31
  • @AlexanderDobernig what exactly it will have to look like, will always depend on the specific, but https://stackoverflow.com/a/38453365/1427878, https://stackoverflow.com/a/11636439/1427878 illustrate the basic principle. Main part is that `%{THE_REQUEST}` env variable is used to check what the client originally requested. – CBroe Jan 21 '21 at 08:35

2 Answers2

1

You may try these rules in your site root .htaccess:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+files/(\S+)\.pdf[?\s] [NC]
RewriteRule ^ /%1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/files/$1.pdf -f
RewriteRule ^(.+?)/?$ files/$1.pdf [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

With your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteCond %{THE_REQUEST} \s(/files/.*)\.pdf\s [NC]
RewriteRule ^ http://%{HTTP_HOST}%1 [R=301,NE]
RewriteRule ^(.*)/?$ files/$1.pdf [L]
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93