forumsviewer — 2014-02-19T14:32:13-05:00 — #1
I'm working with a semi-old site that has used underscores throughout the base web directory. (how_to_do_this.php, who_is_author_name.php, etc.) Part of the redesign we are going to have categories (how-to, who-is) just as an example. To keep everything organized in the base directory we were going to change the naming convention for the URL to "folder-in-url / file-name-in-url.php" with filenames as "folder-in-url_file-name-in-url.php". Notice the underscore is the seperater for the URL so the stuff before the underscore is essentially a folder according to the URL.
ORIGINAL FILENAME -> RENAMED FILENAME -> URL
folder_in_url_file_name_in_url.php -> folder-in-url_file-name-in-url.php -> /folder-in-url/file-name-in-url.php
how_to_do_this.php -> how-to_do-this.php -> /how-to/do-this.php
who_is_author_name.php -> who-is_author-name.php -> /who-is/author-name.php
Is this possible using .htaccess / mod-rewrite? In other words, when someone goes to example.com/how-to/do-this.php then apache will look and present the content of the file "how-to_do-this.php" rather than looking in the how-to folder for do-this.php? Please note that when someone goes to the example.com/how-to/do-this.php it does NOT redirect them to example.com/how-to_do-this.php.
Any help is greatly appreciated. At the end of the day, we can make the folders and put the files in their appropriate folders but then the base web directory will be chuck full of folders whereas for maintenance it would be easier just to administer within the base directory using appropriate filenames with an underscore separating for URL purposes.
Best practices is preferred, so if this is a "hack" and not recommended then we can do folders and put the files within the appropriate "category" folders. Thanks!
forumsviewer — 2014-02-19T16:19:34-05:00 — #2
I found the way to do this in .htaccess but still wondering if this is a good practice.
Here is my htaccess:
RewriteRule ^([a-z-]+)/([a-z-]+)(.*)$ $1_$2$3
dklynn — 2014-02-20T18:01:08-05:00 — #3
What you use as a marker is (almost) irrelevant ... so long as it can't be confused with adjacent character sets. What you have looks okay (but will still have to be redirected to your handler file afterward).
That said, I'm REALLY an advocate of eliminating the indiscriminate use of the :kaioken: EVERYTHING :kaioken: (or NOTHING) atom as it will generally create loopy code. In your case (from the example, (.*) is simply used to capture .php so why not use \.php? That's far more succinct and will eliminate any potential attack vectors, too. When you can use code to restrict requests to what your scripts can accept (as valid input), you're far ahead of the game.