0

I've seen several answers but none of them have worked for me.

How do I rewrite the below using my .htaccess? mod_rewrite is on in my file.

http://example.com/local/vendor/index.php?handle=Company

to be:

http://example.com/local/vendor/Company
MrWhite
  • 23,175
  • 4
  • 44
  • 71
greeves
  • 13
  • 1
  • You are expected to **try to write the code yourself**. After [**doing more research**](https://meta.stackoverflow.com/q/261592/1011527) if you have a problem **post what you've tried** with a **clear explanation of what isn't working** and provide [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Read [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527). – Jay Blanchard Aug 10 '17 at 16:10
  • Jay - I'm new to programming and I'm just trying to get an answer so I can get a site live. I expected a comment like yours. Appreciate any help that can be provided. – greeves Aug 10 '17 at 16:16
  • Possible duplicate of [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](https://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) – Jay Blanchard Aug 10 '17 at 16:17

1 Answers1

0

How do I rewrite the below...

There's possibly a more fundamental problem here. You don't rewrite it that way round in .htaccess. You rewrite from the "friend" URL back to the actual URL/filesystem path. So, it's the other way round, you rewrite from:

http://example.com/local/vendor/Company

to the actual URL your application is expecting:

http://example.com/local/vendor/index.php?handle=Company

For Example:

RewriteEngine On

RewriteRule ^(local/vendor)/(Company)$ /$1/index.php?handle=$2 [L]

$1 and $2 are backreferences to the captured groups in the RewriteRule pattern.

The above matches just the specific URL you stated, ie. /local/vendor/Company. I suspect that "Company" is intended to be a placeholder? In which case you need to make this more general. For example:

RewriteRule ^(local/vendor)/(\w+)$ /$1/index.php?handle=$2 [L]

\w is a shorthand character class for word characters. This excludes the dot (ie. .) so avoids a rewrite loop when rewriting to index.php.


UPDATE: The above assumes your .htaccess file is located in the document root, ie. example.com/.htaccess. However, if the .htaccess file is located in the /local subdirectory (ie. example.com/local/.htaccess) - as you appear to suggest in comments - then you will need to adjust the above directives to remove local/ from the RewriteRule pattern and remove the slash prefix from the substitution. For example:

RewriteRule ^(vendor)/(\w+)$ $1/index.php?handle=$2 [L]
MrWhite
  • 23,175
  • 4
  • 44
  • 71
  • thanks for the quick reply. I tried this and it still didn't work. Maybe I should have said that "local" is my document root. Does that change anything? When i type http://localhost/local/vendor/Company in my browser, it gives me a 404. you are correct in that Company is a placeholder. It would be replaced with a vendor handle the 404 i get is "The requested URL /local/vendor/Company was not found on this server." – greeves Aug 10 '17 at 17:52
  • Bit of a terminology issue I think... if "local" was your _document root_ (I mean really your "document root") then it shouldn't be in your URL at all. (The document root is the filesystem path that defines the root of your public files - it is a config directive in the server config.) If "local" is simply the subdirectory from which you are serving files and is the location of this `.htaccess` file, then you should remove `local/` from the above directives. – MrWhite Aug 10 '17 at 18:29
  • I tried the updated rule and I'm still getting a "404 Not Found" with the following error message: The requested URL /local/vendor/Company was not found on this server (where I'm replacing the actual vendor handle w/ Company as a placeholder). Maybe I should better explain my set up: - Running MAMP stack on my machine - In my htdocs folder, I have a folder titled "local", which all of my files for this particular website are in. There are several subdirectories within "local" and "vendor" is one of them w/ its index.php file. Any thoughts on why I still can't do this? – greeves Aug 11 '17 at 10:43
  • I've just updated the regex slightly, it should really be `^(vendor)/(\w+)$`, as opposed to `^(vendor/)(\w+)$` (the slash should be outside the captured group) - however, that shouldn't actually make any noticeable difference. Just to confirm, did you also change the _substitution_ to read `$1/index.php?handle=$2` (ie. no slash prefix)? Is mod_rewrite enabled/working at all? At the top of your `.htaccess` file try: `RewriteRule ^foo$ http://example.com [R,L]` - if you now access `/foo` are you redirected to `example.com`? – MrWhite Aug 11 '17 at 11:58
  • 1
    That worked! Thanks so much, I really, really appreciate you seeing me through on this one. – greeves Aug 11 '17 at 14:20
  • You're welcome, glad it's now working. Which bit specifically got it working? (Actually, I see now that the update to the regex to remove the slash from the captured group could be significant - if that was the thing that fixed it?) – MrWhite Aug 11 '17 at 15:31
  • 1
    it was the removal of the "/" in the (vendor/) group. Moving that to outside got me all set. – greeves Aug 11 '17 at 19:44