1

I have the following real URL structure

/folder/folder2/file1.php
/folder/folder3/file2.php

where file1 and file2 could be any PHP file.

I also have

/folder/folder2/folder4/file1.php
/folder/folder3/folder4/file2.php

What I want to is change the URLs so they contain an ID like this:

/folder/12345/folder2/file1.php

What I am trying to do, is create a mod_rewrite rule that gets the ID and runs whatever the URL would be without the ID part

so

/folder/12345/folder2/file1.php

would run

/folder/folder2/file1.php?id=12345

What would the mod_rewrite rule look like to do this?

Also, what happens when I am submitting forms either as POST or GET? Can the mod rewrite work with either the post or the get?

MrWhite
  • 12,647
  • 4
  • 29
  • 41
Lawrence Cooke
  • 167
  • 1
  • 1
  • 7
  • Are you using `.htaccess` or are these directives going to be somewhere in your server config; if so, where (`` container or vHost/Server)? – MrWhite Feb 18 '20 at 17:43

1 Answers1

1

I assume, from your question, that you only want to rewrite URLs of the form:

  • /folder/<id>/folder2/<anyfile.php>
  • /folder/<id>/folder3/<anyfile.php>

where folder, folder2 and folder3 are known subdirectories. But the .php file could be anything.

(This excludes /folder/folder2/folder4/file1.php and /folder/folder3/folder4/file2.php etc.)

The <id> is also assumed to be digits only (as in your example). If there are other constraints, such as always being 5 digits in length, then this should also be included.

You would do something like the following using mod_rewrite:

RewriteEngine On

RewriteRule ^/?(folder)/(\d+)/(folder2|folder3)/([^./]+\.php)$ /$1/$3/$4?id=$2 [QSA,L]

$n are backreferences to the parenthsised subpatterns in the RewriteRule pattern.

The QSA flag will append the query string from the request (if any) - required if you are submitting a form via GET to one of these URLs.

Also, what happens when I am submitting forms either as POST or GET? Can the mod rewrite work with either the post or the get?

Both GET and POST requests will be rewritten.

MrWhite
  • 12,647
  • 4
  • 29
  • 41
  • I have got this to work, however if the page contains a link in the format test then it always returns file not found, if I change the link to the full path, it will then work, is there a way to make those relative links work with the rewrite rule? – Lawrence Cooke Feb 20 '20 at 10:22
  • 1
    Since you are rewritting the URL from/to a different URL-path _depth_ you will need to make sure that all your internal URL-paths are either root-relative or absolute. Or use the `base` tag (perhaps). Whilst it might be theoretcially possible to construct a RewriteRule to "workaround" this for specific URLs, it's not a general fix. See [my answer](https://webmasters.stackexchange.com/a/86458/1243) to the following question on the Webmasters Stack for more information on this: https://webmasters.stackexchange.com/questions/86450/htaccess-rewrite-url-leads-to-missing-css – MrWhite Feb 20 '20 at 11:52