-1

Having some issues understanding the .htaccess file and getting it to work properly. .htaccess is recognized....i entered plain test at the top and got the internal server error. And it appears mod-rewrite is working...because I downloaded a test php file and it works.

I am using Apache 2.4.23 with no php pages

However i cant seem to get this to work right.

I am trying to take this url

http://example.com/ProjectTest/index.shtml?dynContent=Content1

and rewrite it like this

http://example.com/ProjectTest/Content1

I am also trying to do this generically across the site since ?dynContent=whatever will be a constant.....and trying to keep the same path.....like the below example

Change this

http://example.com/ProjectTest/ProjectFolder/index.shtml?dynContent=Content1

to this

http://example.com/ProjectTest/ProjectFolder/Content1

I am not very good with the .htaccess files

UPDATE: Ok...I got this to work.....not really sure why it works....but when I type in the test-dev/ProjectTest/apples or test-dev/ProjectTest/oranges it calls up the appropriate content.

This is what I used

RewriteRule ^ProjectTest/([^/\.]+)/?$ ProjectTest/index.shtml?dynContent=$1 [L]

and then I rewrote it to be this and now it works for any directory

RewriteRule ^(.*)/([^/\.]+)/?$ $1/index.shtml?dynContent=$2 [L]
SteveL
  • 77
  • 9

1 Answers1

0

Try this:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^dynContent=(.+)$
RewriteRule ^/(.+)+/index.shtml $1/%1 [PT]

This requires mod_rewrite to be activated. The value of the bracket in dynContent=(.+) is stored in %1 and the bracket's value in ^/(.+)+/index.shtml is stored in $1. Both together creates your target path http://example.com/ProjectTest/ProjectFolder/Content1

Hello Fishy
  • 729
  • 5
  • 16
  • I corrected the index.shtml misspelling and tried it and it didn't work for me....I also create a phpinfo page and checked the modules that were loaded and mod_rewrite was loaded...below is the info from the phpinfo page. – SteveL Apr 06 '17 at 01:52
  • Server Root D:/wamp64/bin/apache/apache2.4.23 Loaded Modules core mod_win32 mpm_winnt http_core mod_so mod_access_compat mod_actions mod_alias mod_allowmethods mod_asis mod_auth_basic mod_auth_digest mod_authn_core mod_authn_file mod_authz_core mod_authz_groupfile mod_authz_host mod_authz_user mod_autoindex mod_cache mod_cache_disk mod_cgi mod_dir mod_env mod_file_cache mod_headers mod_include mod_isapi mod_log_config mod_mime mod_negotiation mod_rewrite mod_setenvif mod_userdir mod_vhost_alias mod_php5 – SteveL Apr 06 '17 at 01:56
  • What exactly did go wrong? error message? log entries? something to work with? Because the rule itself should be correct :/ – Hello Fishy Apr 06 '17 at 07:03
  • Yes..it looks like it should work.....been doing some reading on differences between Apache 2.2 and 2.4 mod_rewrite. Saw something last night where is was suggested to use mod_rewrite in the virtual host directory. Newbie to writing .htaccess....will have to find and look at the logs if any....or what I have to do to turn turn that on. Will try that after work today. – SteveL Apr 06 '17 at 11:50
  • Let me know if you have any specific questions, we are running on both, Apache 2.2 and 2.4. If you have full access to your server you won't need and shouldn't use .htaccess, because it only increases complexity if you don't really need it. Stick to the standard config files. To turn on rewrite log: Search for `LogLevel` in your config, which probably is set to `error` and add the rewrite:trace in that line like this: `LogLevel error rewrite:trace2`. Then restart and test again, rewrites will appear in error.log (search with `tail -f error_log|fgrep '[rewrite:' `) Trace2 should be enough. – Hello Fishy Apr 06 '17 at 11:56
  • Ok...this is what I see [Thu Apr 06 18:23:26.019140 2017] [rewrite:trace3] [pid 16684:tid 1188] mod_rewrite.c(477): [client 127.0.0.1:62514] 127.0.0.1 - - [test-dev/sid#1f4e7367798][rid#1f4e94b54f0/initial] [perdir D:/wamp64/www/test-dev/] strip per-dir prefix: D:/wamp64/www/test-dev/ProjectTest/index.shtml -> ProjectTest/index.shtml – SteveL Apr 06 '17 at 22:28
  • [Thu Apr 06 18:23:26.019642 2017] [rewrite:trace3] [pid 16684:tid 1188] mod_rewrite.c(477): [client 127.0.0.1:62514] 127.0.0.1 - - [test-dev/sid#1f4e7367798][rid#1f4e94b54f0/initial] [perdir D:/wamp64/www/test-dev/] applying pattern '^/(.*)/index.shtml' to uri 'ProjectTest/index.shtml' [Thu Apr 06 18:23:26.019642 2017] [rewrite:trace3] [pid 16684:tid 1188] mod_rewrite.c(477): [client 127.0.0.1:62514] 127.0.0.1 - - [test-dev/sid#1f4e7367798][rid#1f4e94b54f0/initial] [perdir D:/wamp64/www/test-dev/] strip per-dir prefix: D:/wamp64/www/test-dev/ProjectTest/index.shtml -> ProjectTest/index.shtml – SteveL Apr 06 '17 at 22:28
  • [Thu Apr 06 18:23:26.019642 2017] [rewrite:trace3] [pid 16684:tid 1188] mod_rewrite.c(477): [client 127.0.0.1:62514] 127.0.0.1 - - [test-dev/sid#1f4e7367798][rid#1f4e94b54f0/initial] [perdir D:/wamp64/www/test-dev/] applying pattern '^/(.*)/index.shtml' to uri 'ProjectTest/index.shtml' [Thu Apr 06 18:23:26.019642 2017] [rewrite:trace1] [pid 16684:tid 1188] mod_rewrite.c(477): [client 127.0.0.1:62514] 127.0.0.1 - - [test-dev/sid#1f4e7367798][rid#1f4e94b54f0/initial] [perdir D:/wamp64/www/test-dev/] pass through D:/wamp64/www/test-dev/ProjectTest/index.shtml – SteveL Apr 06 '17 at 22:28
  • My url is still this http://test-dev/ProjectTest/index.shtml?dynContent=apples instead of this http://test-dev/ProjectTest/apples – SteveL Apr 06 '17 at 22:31
  • Here is my vhost directory for the test-dev site Options +Includes AllowOverride all Require all granted AddType text/html .shtml AddHandler server-parsed .shtml AddHandler cgi-script .cgi .pl SSILegacyExprParser on DirectoryIndex default.shtml index.shtml default.php index.php index.html RewriteEngine on RewriteCond %{QUERY_STRING} ^dynContent=(.*)$ RewriteRule ^/(.*)/index.shtml http://test-dev/ProjectTest/$1/%1 [R=301,L] – SteveL Apr 06 '17 at 22:48
  • Ok...making some progress....I got this to work....as it pulled up the apples page http://test-dev/test RewriteRule ^test/?$ /ProjectTest/index.shtml?dc=apples [PT] – SteveL Apr 07 '17 at 02:01
  • Can you paste your vhost in your original question? It's REALLY hard to read like this :D And plz also elaborate a little on what exactly worked or if it was the full answer or just a bit of it...thx :) – Hello Fishy Apr 07 '17 at 06:49
  • Added the info to the original question – SteveL Apr 07 '17 at 11:59
  • updated my answer. if that's not working, i don't now what is^^ – Hello Fishy Apr 07 '17 at 12:25
  • Ok...I got this to work.....not really sure why it works....but when I type in the http://test-dev/ProjectTest/apples or http://test-dev/ProjectTest/oranges it calls up the appropriate content. This is what I used RewriteRule ^ProjectTest/([^/\.]+)/?$ ProjectTest/index.shtml?dynContent=$1 [L] – SteveL Apr 07 '17 at 14:58
  • Huh oO ok...can you edit your original post and add that at the end? Would appreciate that :) And still don't understand why the one I gave you didn't work...anything in the rewrite log (within error.log)? – Hello Fishy Apr 07 '17 at 15:55
  • Done...posted in original under update. Nothing really in the error logs other than what I had posted previously. I really need to get a mod_rewrite book or something....its interesting and I would like to learn more about it. – SteveL Apr 07 '17 at 19:47
  • Thanka :) Was just wondering now that you presented a working solution that does exactly the opposite of what your original question was. So pretty much turn myb answer around and you got it^^ nvm,glad you got it to finally – Hello Fishy Apr 08 '17 at 05:48