3

After trawling through posts I have this

RewriteCond %{HTTP_HOST} ^example.software$
RewriteRule ^downloads$ https://example.software\?mode\=downloads&%{QUERY_STRING}  [R=302,L]

This works fine to convert

example.software/downloads?name=bing

to

example.software?mode=downloads&name=bing

And I could make that work, but what I really want is to convert

example.software/downloads/bing

to

example.software?mode=downloads&name=bing    
2
RewriteCond %{HTTP_HOST} ^example.software$
RewriteRule ^downloads$ https://example.software\?mode\=downloads&%{QUERY_STRING}  [R=302,L]

This is an external redirect. The URL is changed to /?mode=downloads&name=bing - which is what the user will see. The idea of implementing a "friendly" URL like /downloads/bing is so this is the only URL that the user sees and the underlying file path that actually handles the request is hidden from the user. If you are going to "redirect" the request to the "ugly" URL (the file path that handles the request) then there is not much point in implementing the "friendly" URL to begin with (except maybe for "sharing").

(You are also unnecessarily backslash-escaping ? and = in the substitution string - this is an "ordinary" string, not a regex. There should also be a slash after the hostname if you are redirecting.)

I'm assuming that the file that actually handles the request is index.php in the document root.

Try the following instead:

RewriteCond %{HTTP_HOST} ^example.software$
RewriteRule ^(downloads)/([^/]+)$ index.php?mode=$1&name=$2 [L]

This internally rewrites (the URL in the browser does not change) a request for example.software/downloads/bing to /index.php?mode=downloads&name=bing.

$1 and $2 are backreferences to the first and second capturing groups in the RewriteRule pattern. $1 is always downloads, but $2 is whatever is in the second path segment.

The URL in the browser's address bar remains as /downloads/bing, but your index.php script is called with the mode and name URL (GET) parameters.

6
  • It doesnt seem to be working at all. If I dont have the nested folder name, then I get a listing of the directory downloads. Which is good, because cpanel was misleading me into thinking as no-indexing. I have fixed that now - so it gives a 403 Oct 20 at 2:13
  • What happens exactly when you request /downloads/bing? An error? Undesirable response? Unstyled page? Nothing? This rule will only rewrite /downloads/<something>, it won't rewrite /downloads (or /downloads/) - which would be problematic since downloads is also a physical directory. If you request /index.php?mode=downloads&name=bing directly does it work? I'm assuming your script is named index.php - is that correct?
    – MrWhite
    Oct 20 at 9:43
  • The file is index.html itself, and I had changed the index.php to index.html. If I type in ?mode=downloads&name=bing, the code detects it and displays it (for now). But if I type /downloads/bing, it runs the index.html but doesnt run the javascript in it. Oct 25 at 11:31
  • But if I change it to external reference RewriteRule ^(downloads)/([^/]+)$ example.com?mode=$1&name=$2 [L] Then it works Oct 25 at 11:37
  • It sounds like you are using relative links/URLs to your JavaScript files. You need to use root-relative (or absolute) URLs. See this related Webmasters question: .htaccess rewrite URL leads to missing CSS
    – MrWhite
    Oct 25 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.